Add android service restart

This commit is contained in:
problematicconsumer
2023-09-10 20:19:36 +03:30
parent 830a5cd75f
commit 778db2dd83
2 changed files with 33 additions and 8 deletions

View File

@@ -50,7 +50,7 @@ class EventHandler : FlutterPlugin {
Log.d(TAG, "new alert: $it")
val map = listOf(
Pair("status", it.status.name),
Pair("failure", it.alert?.name),
Pair("alert", it.alert?.name),
Pair("message", it.message)
)
.mapNotNull { p -> p.second?.let { Pair(p.first, p.second) } }

View File

@@ -2,12 +2,14 @@ package com.hiddify.hiddify
import androidx.annotation.NonNull
import com.hiddify.hiddify.bg.BoxService
import com.hiddify.hiddify.constant.Status
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMethodCodec
import io.nekohasekai.libbox.Libbox
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler {
@@ -18,10 +20,10 @@ class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler {
enum class Trigger(val method: String) {
ParseConfig("parse_config"),
SetActiveConfigPath("set_active_config_path"),
ChangeConfigOptions("change_config_options"),
Start("start"),
Stop("stop"),
Restart("restart"),
SelectOutbound("select_outbound"),
UrlTest("url_test"),
}
@@ -55,12 +57,6 @@ class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler {
}
}
Trigger.SetActiveConfigPath.method -> {
val args = call.arguments as Map<*, *>
Settings.activeConfigPath = args["path"] as String? ?: ""
result.success(true)
}
Trigger.ChangeConfigOptions.method -> {
result.runCatching {
val args = call.arguments as String
@@ -70,6 +66,8 @@ class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler {
}
Trigger.Start.method -> {
val args = call.arguments as Map<*, *>
Settings.activeConfigPath = args["path"] as String? ?: ""
MainActivity.instance.startService()
result.success(true)
}
@@ -79,6 +77,33 @@ class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler {
result.success(true)
}
Trigger.Restart.method -> {
GlobalScope.launch {
result.runCatching {
val args = call.arguments as Map<*, *>
Settings.activeConfigPath = args["path"] as String? ?: ""
val mainActivity = MainActivity.instance
val started = mainActivity.serviceStatus.value == Status.Started
if (!started) return@launch success(true)
val restart = Settings.rebuildServiceMode()
if (restart) {
mainActivity.reconnect()
BoxService.stop()
delay(200)
mainActivity.startService()
success(true)
return@launch
}
runCatching {
Libbox.newStandaloneCommandClient().serviceReload()
success(true)
}.onFailure {
error(it)
}
}
}
}
Trigger.SelectOutbound.method -> {
GlobalScope.launch {
result.runCatching {