Commit d6d89688 authored by Mygod's avatar Mygod

Deprecate using Handler

parent d988777e
......@@ -24,9 +24,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.RemoteException
import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.bg.ProxyService
......@@ -35,13 +33,14 @@ import com.github.shadowsocks.bg.VpnService
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Action
import com.github.shadowsocks.utils.Key
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
/**
* This object should be compact as it will not get GC-ed.
*/
class ShadowsocksConnection(private val handler: Handler = Handler(Looper.getMainLooper()),
private var listenForDeath: Boolean = false) :
ServiceConnection, IBinder.DeathRecipient {
class ShadowsocksConnection(private var listenForDeath: Boolean = false) : ServiceConnection, IBinder.DeathRecipient {
companion object {
val serviceClass get() = when (DataStore.serviceMode) {
Key.modeProxy -> ProxyService::class
......@@ -70,15 +69,17 @@ class ShadowsocksConnection(private val handler: Handler = Handler(Looper.getMai
private val serviceCallback = object : IShadowsocksServiceCallback.Stub() {
override fun stateChanged(state: Int, profileName: String?, msg: String?) {
val callback = callback ?: return
handler.post { callback.stateChanged(BaseService.State.values()[state], profileName, msg) }
GlobalScope.launch(Dispatchers.Main.immediate) {
callback.stateChanged(BaseService.State.values()[state], profileName, msg)
}
}
override fun trafficUpdated(profileId: Long, stats: TrafficStats) {
val callback = callback ?: return
handler.post { callback.trafficUpdated(profileId, stats) }
GlobalScope.launch(Dispatchers.Main.immediate) { callback.trafficUpdated(profileId, stats) }
}
override fun trafficPersisted(profileId: Long) {
val callback = callback ?: return
handler.post { callback.trafficPersisted(profileId) }
GlobalScope.launch(Dispatchers.Main.immediate) { callback.trafficPersisted(profileId) }
}
}
private var binder: IBinder? = null
......@@ -117,7 +118,7 @@ class ShadowsocksConnection(private val handler: Handler = Handler(Looper.getMai
override fun binderDied() {
service = null
callbackRegistered = false
callback?.also { handler.post(it::onBinderDied) }
callback?.also { GlobalScope.launch(Dispatchers.Main.immediate) { it.onBinderDied() } }
}
private fun unregisterCallback() {
......
......@@ -26,8 +26,6 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.VpnService
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
import android.view.*
import androidx.appcompat.app.AppCompatActivity
......@@ -131,8 +129,7 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPref
else -> Core.startService()
}
private val handler = Handler(Looper.getMainLooper())
private val connection = ShadowsocksConnection(handler, true)
private val connection = ShadowsocksConnection(true)
override fun onServiceConnected(service: IShadowsocksService) = changeState(try {
BaseService.State.values()[service.state]
} catch (_: RemoteException) {
......@@ -189,7 +186,7 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPref
override fun onPreferenceDataStoreChanged(store: PreferenceDataStore, key: String) {
when (key) {
Key.serviceMode -> handler.post {
Key.serviceMode -> {
connection.disconnect(this)
connection.connect(this, this)
}
......@@ -268,6 +265,5 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPref
DataStore.publicStore.unregisterChangeListener(this)
connection.disconnect(this)
BackupManager(this).dataChanged()
handler.removeCallbacksAndMessages(null)
}
}
......@@ -26,8 +26,6 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.VpnService
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
import android.text.format.Formatter
import android.widget.Toast
......@@ -118,8 +116,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo
}
}
private val handler = Handler(Looper.getMainLooper())
private val connection = ShadowsocksConnection(handler, true)
private val connection = ShadowsocksConnection(true)
override fun onServiceConnected(service: IShadowsocksService) = changeState(try {
BaseService.State.values()[service.state]
} catch (_: RemoteException) {
......@@ -193,7 +190,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo
override fun onPreferenceDataStoreChanged(store: PreferenceDataStore, key: String) {
when (key) {
Key.serviceMode -> handler.post {
Key.serviceMode -> {
connection.disconnect(requireContext())
connection.connect(requireContext(), this)
}
......
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