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
@@ -73,6 +73,10 @@ trait BaseService {
callbacks.unregister(cb)
callbackCount -= 1
}
if (callbackCount == 0 && timer != null) {
timer.cancel()
timer = null
}
if (callbackCount == 0 && state != State.CONNECTING && state != State.CONNECTED) {
stopBackgroundService()
}
@@ -80,6 +84,18 @@ trait BaseService {
override def registerCallback(cb: IShadowsocksServiceCallback) {
if (cb != null) {
if (callbackCount == 0 && timer == null) {
val task = new TimerTask {
def run {
TrafficMonitor.updateRate()
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
}
}
timer = new Timer(true)
timer.schedule(task, 1000, 1000)
}
callbacks.register(cb)
callbackCount += 1
}
@@ -102,30 +118,19 @@ trait BaseService {
this.config = config
TrafficMonitor.reset()
trafficMonitorThread = new TrafficMonitorThread(this)
trafficMonitorThread = new TrafficMonitorThread()
trafficMonitorThread.start()
val task = new TimerTask {
def run {
clearChildProcessStream()
}
}
timer = new Timer(true)
timer.schedule(task, 60 * 1000, 60 * 1000)
}
def stopRunner() {
// Make sure update total traffic when stopping the runner
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
TrafficMonitor.reset()
if (trafficMonitorThread != null) {
trafficMonitorThread.stopThread()
trafficMonitorThread = null
}
if (timer != null) {
timer.cancel()
timer = null
}
}
def updateTrafficTotal(tx: Long, rx: Long) {
@@ -136,7 +141,6 @@ trait BaseService {
case Some(profile) =>
profile.tx += tx
profile.rx += rx
Log.d("test", profile.tx + " " + profile.rx)
ShadowsocksApplication.profileManager.updateProfile(profile)
case None => // Ignore
}
@@ -144,7 +148,6 @@ trait BaseService {
})
}
def clearChildProcessStream()
def stopBackgroundService()
def getServiceMode: Int
def getTag: String