Commit 8ed652eb authored by Mygod's avatar Mygod

Replace TimerTask with Handler

According to the Internet: https://stackoverflow.com/q/20330355/2245107
Handler is better than TimerTask.
parent e458a76d
...@@ -28,6 +28,7 @@ import android.content.IntentFilter ...@@ -28,6 +28,7 @@ import android.content.IntentFilter
import android.os.Build import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.RemoteCallbackList import android.os.RemoteCallbackList
import android.os.SystemClock
import android.util.Base64 import android.util.Base64
import android.util.Log import android.util.Log
import androidx.core.os.UserManagerCompat import androidx.core.os.UserManagerCompat
...@@ -81,7 +82,6 @@ object BaseService { ...@@ -81,7 +82,6 @@ object BaseService {
@Volatile var pluginPath: String? = null @Volatile var pluginPath: String? = null
val processes = GuardedProcessPool() val processes = GuardedProcessPool()
var timer: Timer? = null
var trafficMonitorThread: TrafficMonitorThread? = null var trafficMonitorThread: TrafficMonitorThread? = null
val callbacks = RemoteCallbackList<IShadowsocksServiceCallback>() val callbacks = RemoteCallbackList<IShadowsocksServiceCallback>()
...@@ -103,15 +103,13 @@ object BaseService { ...@@ -103,15 +103,13 @@ object BaseService {
override fun registerCallback(cb: IShadowsocksServiceCallback) { override fun registerCallback(cb: IShadowsocksServiceCallback) {
callbacks.register(cb) callbacks.register(cb)
} }
override fun startListeningForBandwidth(cb: IShadowsocksServiceCallback) {
if (bandwidthListeners.add(cb.asBinder())) { private fun registerTimeout() =
if (timer == null) { Core.handler.postAtTime(this::onTimeout, this, SystemClock.uptimeMillis() + 1000)
val t = Timer(true) private fun onTimeout() {
t.schedule(object : TimerTask() { val profile = profile
override fun run() { if (profile != null && state == CONNECTED && TrafficMonitor.updateRate() &&
val profile = profile ?: return bandwidthListeners.isNotEmpty()) {
if (state == CONNECTED && TrafficMonitor.updateRate()) Core.handler.post {
if (bandwidthListeners.isNotEmpty()) {
val txRate = TrafficMonitor.txRate val txRate = TrafficMonitor.txRate
val rxRate = TrafficMonitor.rxRate val rxRate = TrafficMonitor.rxRate
val txTotal = TrafficMonitor.txTotal val txTotal = TrafficMonitor.txTotal
...@@ -126,11 +124,13 @@ object BaseService { ...@@ -126,11 +124,13 @@ object BaseService {
} }
callbacks.finishBroadcast() callbacks.finishBroadcast()
} }
registerTimeout()
} }
}
}, 1000, 1000) override fun startListeningForBandwidth(cb: IShadowsocksServiceCallback) {
timer = t val wasEmpty = bandwidthListeners.isEmpty()
} if (bandwidthListeners.add(cb.asBinder())) {
if (wasEmpty) registerTimeout()
TrafficMonitor.updateRate() TrafficMonitor.updateRate()
if (state == CONNECTED) cb.trafficUpdated(profile!!.id, if (state == CONNECTED) cb.trafficUpdated(profile!!.id,
TrafficMonitor.txRate, TrafficMonitor.rxRate, TrafficMonitor.txRate, TrafficMonitor.rxRate,
...@@ -140,8 +140,7 @@ object BaseService { ...@@ -140,8 +140,7 @@ object BaseService {
override fun stopListeningForBandwidth(cb: IShadowsocksServiceCallback) { override fun stopListeningForBandwidth(cb: IShadowsocksServiceCallback) {
if (bandwidthListeners.remove(cb.asBinder()) && bandwidthListeners.isEmpty()) { if (bandwidthListeners.remove(cb.asBinder()) && bandwidthListeners.isEmpty()) {
timer!!.cancel() Core.handler.removeCallbacksAndMessages(this)
timer = null
} }
} }
......
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