Commit 0ec69f23 authored by Mygod's avatar Mygod

Allow data to be null for destroyed services

parent 23e6984c
...@@ -106,8 +106,8 @@ object BaseService { ...@@ -106,8 +106,8 @@ object BaseService {
private val bandwidthListeners = mutableMapOf<IBinder, Long>() // the binder is the real identifier private val bandwidthListeners = mutableMapOf<IBinder, Long>() // the binder is the real identifier
private val handler = Handler() private val handler = Handler()
override fun getState(): Int = data!!.state.ordinal override fun getState(): Int = (data?.state ?: State.Idle).ordinal
override fun getProfileName(): String = data!!.proxy?.profile?.name ?: "Idle" override fun getProfileName(): String = data?.proxy?.profile?.name ?: "Idle"
override fun registerCallback(cb: IShadowsocksServiceCallback) { override fun registerCallback(cb: IShadowsocksServiceCallback) {
callbacks.register(cb) callbacks.register(cb)
...@@ -133,7 +133,7 @@ object BaseService { ...@@ -133,7 +133,7 @@ object BaseService {
.map { Pair(it.profile.id, it.trafficMonitor?.requestUpdate()) } .map { Pair(it.profile.id, it.trafficMonitor?.requestUpdate()) }
.filter { it.second != null } .filter { it.second != null }
.map { Triple(it.first, it.second!!.first, it.second!!.second) } .map { Triple(it.first, it.second!!.first, it.second!!.second) }
if (stats.any { it.third } && data!!.state == State.Connected && bandwidthListeners.isNotEmpty()) { if (stats.any { it.third } && data?.state == State.Connected && bandwidthListeners.isNotEmpty()) {
val sum = stats.fold(TrafficStats()) { a, b -> a + b.second } val sum = stats.fold(TrafficStats()) { a, b -> a + b.second }
broadcast { item -> broadcast { item ->
if (bandwidthListeners.contains(item.asBinder())) { if (bandwidthListeners.contains(item.asBinder())) {
...@@ -149,16 +149,17 @@ object BaseService { ...@@ -149,16 +149,17 @@ object BaseService {
val wasEmpty = bandwidthListeners.isEmpty() val wasEmpty = bandwidthListeners.isEmpty()
if (bandwidthListeners.put(cb.asBinder(), timeout) == null) { if (bandwidthListeners.put(cb.asBinder(), timeout) == null) {
if (wasEmpty) registerTimeout() if (wasEmpty) registerTimeout()
if (data!!.state != State.Connected) return if (data?.state != State.Connected) return
var sum = TrafficStats() var sum = TrafficStats()
val proxy = data!!.proxy ?: return val data = data
val proxy = data?.proxy ?: return
proxy.trafficMonitor?.out.also { stats -> proxy.trafficMonitor?.out.also { stats ->
cb.trafficUpdated(proxy.profile.id, if (stats == null) sum else { cb.trafficUpdated(proxy.profile.id, if (stats == null) sum else {
sum += stats sum += stats
stats stats
}) })
} }
data!!.udpFallback?.also { udpFallback -> data.udpFallback?.also { udpFallback ->
udpFallback.trafficMonitor?.out.also { stats -> udpFallback.trafficMonitor?.out.also { stats ->
cb.trafficUpdated(udpFallback.profile.id, if (stats == null) TrafficStats() else { cb.trafficUpdated(udpFallback.profile.id, if (stats == null) TrafficStats() else {
sum += stats sum += stats
......
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