Commit d6d89688 authored by Mygod's avatar Mygod

Deprecate using Handler

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