Stop the timer when all callbacks removed

This commit is contained in:
Max Lv
2015-11-21 18:26:14 +08:00
parent 6e974da8ca
commit 656bb28a11
7 changed files with 52 additions and 52 deletions
@@ -39,20 +39,23 @@ object TrafficMonitor {
else numberFormat.format(n) + ' ' + units(i)
}
def update(tx: Long, rx: Long) {
val now = getTraffic(tx, rx)
def updateRate() {
val now = getTraffic(txTotal, rxTotal)
val delta = now.timestamp - last.timestamp
txLast = now.tx - last.tx
rxLast = now.rx - last.rx
val deltaTx = now.tx - last.tx
val deltaRx = now.rx - last.rx
if (delta != 0) {
txRate = txLast * 1000 / delta
rxRate = rxLast * 1000 / delta
txRate = deltaTx * 1000 / delta
rxRate = deltaRx * 1000 / delta
}
txTotal += txLast
rxTotal += rxLast
last = now
}
def update(tx: Long, rx: Long) {
txTotal = tx;
rxTotal = rx;
}
def reset() {
txRate = 0
rxRate = 0
@@ -84,5 +87,17 @@ object TrafficMonitor {
def getRate(): String = {
formatTraffic(txRate + rxRate)
}
def getDeltaTx(): Long = {
val last = txLast
txLast = txTotal
txTotal - last
}
def getDeltaRx(): Long = {
val last = rxLast
rxLast = rxTotal
rxTotal - last
}
}
@@ -46,7 +46,7 @@ import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
import android.util.Log
import com.github.shadowsocks.BaseService
class TrafficMonitorThread(service: BaseService) extends Thread {
class TrafficMonitorThread() extends Thread {
val TAG = "TrafficMonitorThread"
val PATH = "/data/data/com.github.shadowsocks/stat_path"
@@ -108,9 +108,6 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
val stat = new String(array, "UTF-8").split("\\|")
if (stat.length == 2) {
TrafficMonitor.update(stat(0).toLong, stat(1).toLong)
service.updateTrafficTotal(TrafficMonitor.txLast, TrafficMonitor.rxLast)
service.updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
}
output.write(0)