Commit 0f12a7b0 authored by Max Lv's avatar Max Lv

refine speed stat

parent a89d0806
......@@ -44,7 +44,7 @@
<!-- notification category -->
<string name="forward_success">Shadowsocks started.</string>
<string name="service_running">Running in the background.</string>
<string name="service_status" formatted="false">Upload: %d KB/s, Download: %d KB/s</string>
<string name="service_status" formatted="false">Upload: %d KB/s\t|\tDownload: %d KB/s</string>
<string name="forward_fail">Shadowsocks failed to start.</string>
<string name="service_stopped">Shadowsocks stopped.</string>
<string name="forward_stop">Shadowsocks stopped.</string>
......
......@@ -120,8 +120,10 @@ class ShadowsocksService extends Service {
private var state = State.INIT
private var last = new TrafficStat(TrafficStats.getTotalTxBytes,
TrafficStats.getTotalRxBytes, java.lang.System.currentTimeMillis())
private var lastTxRate = 0
private var lastRxRate = 0
private val timer = new Timer(true)
private val TIMER_INTERVAL = 5
private val TIMER_INTERVAL = 1
private def changeState(s: Int) {
if (state != s) {
......@@ -412,13 +414,19 @@ class ShadowsocksService extends Service {
def run() {
val now = new TrafficStat(TrafficStats.getTotalTxBytes,
TrafficStats.getTotalRxBytes, java.lang.System.currentTimeMillis())
val txRate = (now.tx - last.tx) / 1024 / TIMER_INTERVAL
val rxRate = (now.rx - last.rx) / 1024 / TIMER_INTERVAL
val txRate = ((now.tx - last.tx) / 1024 / TIMER_INTERVAL).toInt
val rxRate = ((now.rx - last.rx) / 1024 / TIMER_INTERVAL).toInt
last = now
if (lastTxRate == txRate && lastRxRate == rxRate) {
return
} else {
lastTxRate = txRate
lastRxRate = rxRate
}
if (state == State.CONNECTED) {
Log.d(TAG, getString(R.string.service_status).format(txRate, rxRate))
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_status).format(txRate, rxRate), txRate.toInt + rxRate.toInt)
getString(R.string.service_status).format(txRate, rxRate), txRate + rxRate)
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment