Commit d8ab3733 authored by Mygod's avatar Mygod

Fix getCallbackCount is not available on API 16

parent b6fc013a
...@@ -56,6 +56,7 @@ trait BaseService extends Service { ...@@ -56,6 +56,7 @@ trait BaseService extends Service {
var trafficMonitorThread: TrafficMonitorThread = null var trafficMonitorThread: TrafficMonitorThread = null
final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback] final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback]
var callbacksCount: Int = _
val binder = new IShadowsocksService.Stub { val binder = new IShadowsocksService.Stub {
override def getMode: Int = { override def getMode: Int = {
...@@ -67,19 +68,19 @@ trait BaseService extends Service { ...@@ -67,19 +68,19 @@ trait BaseService extends Service {
} }
override def unregisterCallback(cb: IShadowsocksServiceCallback) { override def unregisterCallback(cb: IShadowsocksServiceCallback) {
if (cb != null ) { if (cb != null && callbacks.unregister(cb)) {
callbacks.unregister(cb) callbacksCount -= 1
} if (callbacksCount == 0 && timer != null) {
if (callbacks.getRegisteredCallbackCount == 0 && timer != null) {
timer.cancel() timer.cancel()
timer = null timer = null
} }
} }
}
override def registerCallback(cb: IShadowsocksServiceCallback) { override def registerCallback(cb: IShadowsocksServiceCallback) {
if (cb != null) { if (cb != null && callbacks.register(cb)) {
callbacks.register(cb) callbacksCount += 1
if (callbacks.getRegisteredCallbackCount != 0 && timer == null) { if (callbacksCount != 0 && timer == null) {
val task = new TimerTask { val task = new TimerTask {
def run { def run {
if (TrafficMonitor.updateRate()) updateTrafficRate() if (TrafficMonitor.updateRate()) updateTrafficRate()
...@@ -160,7 +161,7 @@ trait BaseService extends Service { ...@@ -160,7 +161,7 @@ trait BaseService extends Service {
def updateTrafficRate() { def updateTrafficRate() {
val handler = new Handler(getContext.getMainLooper) val handler = new Handler(getContext.getMainLooper)
handler.post(() => { handler.post(() => {
if (callbacks.getRegisteredCallbackCount > 0) { if (callbacksCount > 0) {
val txRate = TrafficMonitor.getTxRate val txRate = TrafficMonitor.getTxRate
val rxRate = TrafficMonitor.getRxRate val rxRate = TrafficMonitor.getRxRate
val txTotal = TrafficMonitor.getTxTotal val txTotal = TrafficMonitor.getTxTotal
...@@ -181,7 +182,7 @@ trait BaseService extends Service { ...@@ -181,7 +182,7 @@ trait BaseService extends Service {
protected def changeState(s: Int, msg: String) { protected def changeState(s: Int, msg: String) {
val handler = new Handler(getContext.getMainLooper) val handler = new Handler(getContext.getMainLooper)
handler.post(() => if (state != s) { handler.post(() => if (state != s) {
if (callbacks.getRegisteredCallbackCount > 0) { if (callbacksCount > 0) {
val n = callbacks.beginBroadcast() val n = callbacks.beginBroadcast()
for (i <- 0 until n) { for (i <- 0 until n) {
try { try {
......
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