Fix speed display after screen turns on
Also, use the tab to make the text less jumpy
This commit is contained in:
@@ -27,34 +27,37 @@ const val divisor = 1024F
|
||||
fun Long.toSpeedString() = toTrafficString() + "/s"
|
||||
|
||||
fun Long.toTrafficString(): String {
|
||||
if (this == 0L)
|
||||
return "\t\t\t0\t B"
|
||||
|
||||
if (this < threshold)
|
||||
return "$this B"
|
||||
return "${this.toFloat().toShortString()}\t B"
|
||||
|
||||
val kib = this / divisor
|
||||
if (kib < threshold)
|
||||
return "${kib.toShortString()} KB"
|
||||
return "${kib.toShortString()}\t KB"
|
||||
|
||||
val mib = kib / divisor
|
||||
if (mib < threshold)
|
||||
return "${mib.toShortString()} MB"
|
||||
return "${mib.toShortString()}\t MB"
|
||||
|
||||
val gib = mib / divisor
|
||||
if (gib < threshold)
|
||||
return "${gib.toShortString()} GB"
|
||||
return "${gib.toShortString()}\t GB"
|
||||
|
||||
val tib = gib / divisor
|
||||
if (tib < threshold)
|
||||
return "${tib.toShortString()} TB"
|
||||
return "${tib.toShortString()}\t TB"
|
||||
|
||||
val pib = tib / divisor
|
||||
if (pib < threshold)
|
||||
return "${pib.toShortString()} PB"
|
||||
return "${pib.toShortString()}\t PB"
|
||||
|
||||
return "∞"
|
||||
}
|
||||
|
||||
private fun Float.toShortString(): String {
|
||||
val s = toString()
|
||||
val s = "%.2f".format(this)
|
||||
if (s.length <= 4)
|
||||
return s
|
||||
return s.substring(0, 4).removeSuffix(".")
|
||||
|
||||
@@ -52,6 +52,7 @@ class V2RayVpnService : VpnService() {
|
||||
}
|
||||
|
||||
private val v2rayPoint = Libv2ray.newV2RayPoint(V2RayCallback())
|
||||
private var lastQueryTime = 0L
|
||||
private lateinit var configContent: String
|
||||
private lateinit var mInterface: ParcelFileDescriptor
|
||||
val fd: Int get() = mInterface.fd
|
||||
@@ -182,6 +183,7 @@ class V2RayVpnService : VpnService() {
|
||||
// Create a new interface using the builder and save the parameters.
|
||||
mInterface = builder.establish()
|
||||
sendFd()
|
||||
lastQueryTime = System.currentTimeMillis()
|
||||
startSpeedNotification()
|
||||
}
|
||||
|
||||
@@ -383,10 +385,13 @@ class V2RayVpnService : VpnService() {
|
||||
val uplink = v2rayPoint.queryStats("socks", "uplink")
|
||||
val downlink = v2rayPoint.queryStats("socks", "downlink")
|
||||
val zero_speed = (uplink == 0L && downlink == 0L)
|
||||
val queryTime = System.currentTimeMillis()
|
||||
if (!zero_speed || !last_zero_speed) {
|
||||
updateNotification("${cf_name} • ${(uplink / 3).toSpeedString()}↑ ${(downlink / 3).toSpeedString()}↓")
|
||||
updateNotification("${cf_name} • ${(uplink * 1000 / (queryTime - lastQueryTime)).toSpeedString()}↑" +
|
||||
" ${(downlink * 1000 / (queryTime - lastQueryTime)).toSpeedString()}↓")
|
||||
}
|
||||
last_zero_speed = zero_speed
|
||||
lastQueryTime = queryTime
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user