Merge branch 'main' of hiddify-github:hiddify/hiddify-next

This commit is contained in:
Hiddify
2023-10-24 11:26:57 +02:00
parent 45d3243d9e
commit aa946deebd
38 changed files with 668 additions and 1686 deletions

View File

@@ -1,5 +1,6 @@
package com.hiddify.hiddify
import android.util.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
@@ -18,13 +19,15 @@ class LogHandler : FlutterPlugin {
logsChannel.setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
MainActivity.instance.serviceLogs.observeForever {
if (it == null) return@observeForever
events?.success(it)
val activity = MainActivity.instance
events?.success(activity.logList)
activity.logCallback = {
events?.success(activity.logList)
}
}
override fun onCancel(arguments: Any?) {
MainActivity.instance.logCallback = null
}
})
}

View File

@@ -39,7 +39,6 @@ class MainActivity : FlutterFragmentActivity(), ServiceConnection.Callback {
var logCallback: ((Boolean) -> Unit)? = null
val serviceStatus = MutableLiveData(Status.Stopped)
val serviceAlerts = MutableLiveData<ServiceEvent?>(null)
val serviceLogs = MutableLiveData<String?>(null)
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
@@ -102,37 +101,18 @@ class MainActivity : FlutterFragmentActivity(), ServiceConnection.Callback {
serviceAlerts.postValue(ServiceEvent(Status.Stopped, type, message))
}
private var paused = false
override fun onPause() {
super.onPause()
paused = true
}
override fun onResume() {
super.onResume()
paused = false
logCallback?.invoke(true)
}
override fun onServiceWriteLog(message: String?) {
if (paused) {
if (logList.size > 300) {
logList.removeFirst()
}
if (logList.size > 300) {
logList.removeFirst()
}
logList.addLast(message)
if (!paused) {
logCallback?.invoke(false)
serviceLogs.postValue(message)
}
logCallback?.invoke(false)
}
override fun onServiceResetLogs(messages: MutableList<String>) {
logList.clear()
logList.addAll(messages)
if (!paused) logCallback?.invoke(true)
logCallback?.invoke(true)
}
override fun onDestroy() {

View File

@@ -29,6 +29,7 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
Restart("restart"),
SelectOutbound("select_outbound"),
UrlTest("url_test"),
ClearLogs("clear_logs"),
}
}
@@ -63,38 +64,44 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
}
Trigger.ChangeConfigOptions.method -> {
result.runCatching {
val args = call.arguments as String
Settings.configOptions = args
success(true)
scope.launch {
result.runCatching {
val args = call.arguments as String
Settings.configOptions = args
success(true)
}
}
}
Trigger.Start.method -> {
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) {
Log.w(TAG, "service is already running")
return success(true)
scope.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) {
Log.w(TAG, "service is already running")
return@launch success(true)
}
mainActivity.startService()
success(true)
}
mainActivity.startService()
success(true)
}
}
Trigger.Stop.method -> {
result.runCatching {
val mainActivity = MainActivity.instance
val started = mainActivity.serviceStatus.value == Status.Started
if (!started) {
Log.w(TAG, "service is not running")
return success(true)
scope.launch {
result.runCatching {
val mainActivity = MainActivity.instance
val started = mainActivity.serviceStatus.value == Status.Started
if (!started) {
Log.w(TAG, "service is not running")
return@launch success(true)
}
BoxService.stop()
success(true)
}
BoxService.stop()
success(true)
}
}
@@ -151,6 +158,15 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
}
}
Trigger.ClearLogs.method -> {
scope.launch {
result.runCatching {
MainActivity.instance.onServiceResetLogs(mutableListOf())
success(true)
}
}
}
else -> result.notImplemented()
}
}