Fix speed display after screen turns on

Also, use the tab to make the text less jumpy
This commit is contained in:
yuhan6665
2020-05-28 21:58:28 -04:00
parent c105d84b35
commit aea8369b8a
2 changed files with 16 additions and 8 deletions

View File

@@ -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(".")

View File

@@ -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
}
}
}