Skip to content

Commit d3c3f04

Browse files
committed
Fix android tile service
Support append system DNS Fix some issues
1 parent 201062d commit d3c3f04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+285
-161
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
<activity
6868
android:name=".TempActivity"
69+
android:excludeFromRecents="true"
6970
android:exported="true"
7071
android:theme="@style/TransparentTheme">
7172
<intent-filter>

android/app/src/main/kotlin/com/follow/clash/BroadcastReceiver.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class BroadcastReceiver : BroadcastReceiver() {
2020

2121
BroadcastAction.SERVICE_DESTROYED.action -> {
2222
GlobalState.log("Receiver service destroyed")
23-
State.handleStopServiceAction()
23+
GlobalState.launch {
24+
State.handleStopServiceAction()
25+
}
2426
}
2527
}
2628
}

android/app/src/main/kotlin/com/follow/clash/State.kt

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,30 @@ object State {
6565
}
6666

6767
suspend fun handleStartServiceAction() {
68-
tilePlugin?.handleStart()
69-
if (flutterEngine != null) {
70-
return
68+
runLock.withLock {
69+
if (runStateFlow.value != RunState.STOP) {
70+
return
71+
}
72+
tilePlugin?.handleStart()
73+
if (flutterEngine != null) {
74+
return
75+
}
76+
startServiceWithEngine()
7177
}
72-
startServiceWithEngine()
78+
7379
}
7480

75-
fun handleStopServiceAction() {
76-
tilePlugin?.handleStop()
77-
if (flutterEngine != null || serviceFlutterEngine != null) {
78-
return
81+
suspend fun handleStopServiceAction() {
82+
runLock.withLock {
83+
if (runStateFlow.value != RunState.START) {
84+
return
85+
}
86+
tilePlugin?.handleStop()
87+
if (flutterEngine != null || serviceFlutterEngine != null) {
88+
return
89+
}
90+
handleStopService()
7991
}
80-
handleStopService()
8192
}
8293

8394
fun handleStartService() {
@@ -90,8 +101,23 @@ object State {
90101
startService()
91102
}
92103

104+
fun handleStopService() {
105+
GlobalState.launch {
106+
runLock.withLock {
107+
if (runStateFlow.value != RunState.START) {
108+
return@launch
109+
}
110+
runStateFlow.tryEmit(RunState.PENDING)
111+
runTime = Service.stopService()
112+
runStateFlow.tryEmit(RunState.STOP)
113+
}
114+
destroyServiceEngine()
115+
}
116+
}
117+
93118
suspend fun destroyServiceEngine() {
94119
runLock.withLock {
120+
GlobalState.log("Destroy service engine")
95121
withContext(Dispatchers.Main) {
96122
runCatching {
97123
serviceFlutterEngine?.destroy()
@@ -101,28 +127,32 @@ object State {
101127
}
102128
}
103129

104-
suspend fun startServiceWithEngine() {
105-
runLock.withLock {
106-
if (serviceFlutterEngine != null || runStateFlow.value == RunState.PENDING || runStateFlow.value == RunState.START) {
107-
return
108-
}
109-
withContext(Dispatchers.Main) {
110-
serviceFlutterEngine = FlutterEngine(GlobalState.application)
111-
serviceFlutterEngine?.plugins?.add(ServicePlugin())
112-
serviceFlutterEngine?.plugins?.add(AppPlugin())
113-
serviceFlutterEngine?.plugins?.add(TilePlugin())
114-
val dartEntrypoint = DartExecutor.DartEntrypoint(
115-
FlutterInjector.instance().flutterLoader().findAppBundlePath(), "_service"
116-
)
117-
serviceFlutterEngine?.dartExecutor?.executeDartEntrypoint(dartEntrypoint)
130+
private fun startServiceWithEngine() {
131+
GlobalState.launch {
132+
runLock.withLock {
133+
if (runStateFlow.value != RunState.STOP) {
134+
return@launch
135+
}
136+
GlobalState.log("Create service engine")
137+
withContext(Dispatchers.Main) {
138+
serviceFlutterEngine?.destroy()
139+
serviceFlutterEngine = FlutterEngine(GlobalState.application)
140+
serviceFlutterEngine?.plugins?.add(ServicePlugin())
141+
serviceFlutterEngine?.plugins?.add(AppPlugin())
142+
serviceFlutterEngine?.plugins?.add(TilePlugin())
143+
val dartEntrypoint = DartExecutor.DartEntrypoint(
144+
FlutterInjector.instance().flutterLoader().findAppBundlePath(), "_service"
145+
)
146+
serviceFlutterEngine?.dartExecutor?.executeDartEntrypoint(dartEntrypoint)
147+
}
118148
}
119149
}
120150
}
121151

122152
private fun startService() {
123153
GlobalState.launch {
124154
runLock.withLock {
125-
if (runStateFlow.value == RunState.PENDING || runStateFlow.value == RunState.START) {
155+
if (runStateFlow.value != RunState.STOP) {
126156
return@launch
127157
}
128158
runStateFlow.tryEmit(RunState.PENDING)
@@ -141,20 +171,6 @@ object State {
141171
}
142172

143173
}
144-
145-
fun handleStopService() {
146-
GlobalState.launch {
147-
runLock.withLock {
148-
if (runStateFlow.value == RunState.PENDING || runStateFlow.value == RunState.STOP) {
149-
return@launch
150-
}
151-
runStateFlow.tryEmit(RunState.PENDING)
152-
runTime = Service.stopService()
153-
runStateFlow.tryEmit(RunState.STOP)
154-
}
155-
destroyServiceEngine()
156-
}
157-
}
158174
}
159175

160176

android/app/src/main/kotlin/com/follow/clash/TempActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class TempActivity : Activity(),
2121
}
2222

2323
QuickAction.STOP.action -> {
24-
State.handleStopServiceAction()
24+
launch {
25+
State.handleStopServiceAction()
26+
}
2527
}
2628

2729
QuickAction.TOGGLE.action -> {
@@ -30,6 +32,6 @@ class TempActivity : Activity(),
3032
}
3133
}
3234
}
33-
finishAndRemoveTask()
35+
finish()
3436
}
3537
}

android/core/src/main/cpp/CMakeLists.txt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,9 @@ message("CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}")
88

99

1010
if (NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
11-
# set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
12-
add_compile_options(-O3)
13-
14-
add_compile_options(-flto)
15-
16-
add_compile_options(-g0)
17-
18-
add_compile_options(-ffunction-sections -fdata-sections)
19-
20-
add_compile_options(-fno-exceptions -fno-rtti)
21-
22-
add_link_options(
23-
-flto
24-
-Wl,--gc-sections
25-
-Wl,--strip-all
26-
-Wl,--exclude-libs=ALL
27-
)
28-
29-
add_compile_options(-fvisibility=hidden -fvisibility-inlines-hidden)
11+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
12+
add_compile_options(-O3 -flto -g0 -fno-exceptions -fno-rtti)
13+
add_link_options(-flto -Wl,--gc-sections,--strip-all)
3014
endif ()
3115

3216
set(LIB_CLASH_PATH "${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libclash.so")

arb/intl_en.arb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,7 @@
432432
"dataCollectionTip": "Data Collection Notice",
433433
"dataCollectionContent": "This app uses Firebase Crashlytics to collect crash information to improve app stability.\nThe collected data includes device information and crash details, but does not contain personal sensitive data.\nYou can disable this feature in settings.",
434434
"crashlytics": "Crash Analysis",
435-
"crashlyticsTip": "When enabled, automatically uploads crash logs without sensitive information when the app crashes"
435+
"crashlyticsTip": "When enabled, automatically uploads crash logs without sensitive information when the app crashes",
436+
"appendSystemDns": "Append System DNS",
437+
"appendSystemDnsTip": "Forcefully append system DNS to the configuration"
436438
}

arb/intl_ja.arb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,7 @@
433433
"dataCollectionTip": "データ収集説明",
434434
"dataCollectionContent": "本アプリはFirebase Crashlyticsを使用してクラッシュ情報を収集し、アプリの安定性を向上させます。\n収集されるデータにはデバイス情報とクラッシュ詳細が含まれますが、個人の機密データは含まれません。\n設定でこの機能を無効にすることができます。",
435435
"crashlytics": "クラッシュ分析",
436-
"crashlyticsTip": "有効にすると、アプリがクラッシュした際に機密情報を含まないクラッシュログを自動的にアップロードします"
436+
"crashlyticsTip": "有効にすると、アプリがクラッシュした際に機密情報を含まないクラッシュログを自動的にアップロードします",
437+
"appendSystemDns": "システムDNSを追加",
438+
"appendSystemDnsTip": "設定にシステムDNSを強制的に追加します"
437439
}

arb/intl_ru.arb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,7 @@
433433
"dataCollectionTip": "Уведомление о сборе данных",
434434
"dataCollectionContent": "Это приложение использует Firebase Crashlytics для сбора информации о сбоях nhằm улучшения стабильности приложения.\nСобираемые данные включают информацию об устройстве и подробности о сбоях, но не содержат персональных конфиденциальных данных.\nВы можете отключить эту функцию в настройках.",
435435
"crashlytics": "Анализ сбоев",
436-
"crashlyticsTip": "При включении автоматически загружает журналы сбоев без конфиденциальной информации, когда приложение выходит из строя"
436+
"crashlyticsTip": "При включении автоматически загружает журналы сбоев без конфиденциальной информации, когда приложение выходит из строя",
437+
"appendSystemDns": "Добавить системный DNS",
438+
"appendSystemDnsTip": "Принудительно добавить системный DNS к конфигурации"
437439
}

arb/intl_zh_CN.arb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,7 @@
433433
"dataCollectionTip": "数据收集说明",
434434
"dataCollectionContent": "本应用使用 Firebase Crashlytics 收集崩溃信息以改进应用稳定性。\n收集的数据包括设备信息和崩溃详情,不包含个人敏感数据。\n您可以在设置中关闭此功能。",
435435
"crashlytics": "崩溃分析",
436-
"crashlyticsTip": "开启后,应用崩溃时自动上传不包含敏感信息的崩溃日志"
436+
"crashlyticsTip": "开启后,应用崩溃时自动上传不包含敏感信息的崩溃日志",
437+
"appendSystemDns": "追加系统DNS",
438+
"appendSystemDnsTip": "强制为配置附加系统DNS"
437439
}

core/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/metacubex/mihomo/tunnel"
2525
"os"
2626
"path/filepath"
27+
"runtime"
2728
"sync"
2829
)
2930

@@ -269,6 +270,7 @@ func setupConfig(params *SetupParams) error {
269270
hub.ApplyConfig(currentConfig)
270271
patchSelectGroup(params.SelectedMap)
271272
updateListeners()
273+
runtime.GC()
272274
return err
273275
}
274276

0 commit comments

Comments
 (0)