diff --git a/service/src/main/java/com/github/kr328/clash/service/BaseService.kt b/service/src/main/java/com/github/kr328/clash/service/BaseService.kt index 8fe94c6..ad69d2a 100644 --- a/service/src/main/java/com/github/kr328/clash/service/BaseService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/BaseService.kt @@ -5,10 +5,10 @@ import android.content.Context import com.github.kr328.clash.common.utils.createLanguageConfigurationContext import com.github.kr328.clash.service.settings.ServiceSettings import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.MainScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel -abstract class BaseService : Service(), CoroutineScope by MainScope() { +abstract class BaseService : Service(), CoroutineScope by CoroutineScope(Dispatchers.Default) { lateinit var settings: ServiceSettings override fun attachBaseContext(base: Context?) { diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt b/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt index 46d4236..0040c9c 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt @@ -8,12 +8,9 @@ import com.github.kr328.clash.service.data.SelectedProxyDao import com.github.kr328.clash.service.data.SelectedProxyEntity import com.github.kr328.clash.service.transact.IStreamCallback import com.github.kr328.clash.service.transact.ParcelableContainer -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch - -class ClashManager(parent: CoroutineScope) : - IClashManager.Stub(), CoroutineScope by parent { +import kotlinx.coroutines.runBlocking +class ClashManager: IClashManager.Stub() { override fun setProxyMode(mode: String?) { Clash.setProxyMode(requireNotNull(mode)) } @@ -29,8 +26,8 @@ class ClashManager(parent: CoroutineScope) : override fun setSelector(proxy: String?, selected: String?) { require(proxy != null && selected != null) - launch { - val current = ProfileDao.queryActive() ?: return@launch + runBlocking { + val current = ProfileDao.queryActive() ?: return@runBlocking SelectedProxyDao.setSelectedForProfile(SelectedProxyEntity(current.id, proxy, selected)) } diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt index 32e921e..a4c92eb 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt @@ -5,6 +5,6 @@ import android.os.IBinder class ClashManagerService : BaseService() { override fun onBind(intent: Intent?): IBinder? { - return ClashManager(this) + return ClashManager() } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index cea8fc7..28ac0a7 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -4,7 +4,10 @@ import android.content.Intent import android.os.Binder import android.os.IBinder import com.github.kr328.clash.service.clash.ClashRuntime -import com.github.kr328.clash.service.clash.module.* +import com.github.kr328.clash.service.clash.module.CloseModule +import com.github.kr328.clash.service.clash.module.DynamicNotificationModule +import com.github.kr328.clash.service.clash.module.ReloadModule +import com.github.kr328.clash.service.clash.module.StaticNotificationModule import com.github.kr328.clash.service.settings.ServiceSettings import com.github.kr328.clash.service.util.broadcastClashStarted import com.github.kr328.clash.service.util.broadcastClashStopped diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt index 67c1c28..f7c833c 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt @@ -4,7 +4,6 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.webkit.URLUtil -import com.github.kr328.clash.common.utils.Log import com.github.kr328.clash.core.Clash import com.github.kr328.clash.service.data.ProfileDao import com.github.kr328.clash.service.model.Profile @@ -17,8 +16,6 @@ import kotlinx.coroutines.future.await import kotlinx.coroutines.withContext import java.io.File import java.io.FileNotFoundException -import java.lang.Exception -import java.lang.NullPointerException import java.util.* object ProfileProcessor { diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index a0ef99b..0258fa9 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -7,11 +7,11 @@ import com.github.kr328.clash.service.clash.module.* import com.github.kr328.clash.service.settings.ServiceSettings import com.github.kr328.clash.service.util.* import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.MainScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel import kotlinx.coroutines.launch -class TunService : VpnService(), CoroutineScope by MainScope() { +class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.Default) { companion object { // from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt private const val VPN_MTU = 9000 diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt index 9f7bb32..7826fa8 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt @@ -9,7 +9,6 @@ import com.github.kr328.clash.core.Clash import com.github.kr328.clash.service.util.parseCIDR import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import java.lang.NullPointerException class TunModule(private val service: VpnService) : Module() { interface Configure {