File tree Expand file tree Collapse file tree
app/src/main/java/com/github/kr328/clash/util
design/src/main/java/com/github/kr328/clash/design/store
service/src/main/java/com/github/kr328/clash/service Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -9,10 +9,11 @@ import com.github.kr328.clash.common.util.intent
99import com.github.kr328.clash.design.store.UiStore
1010import com.github.kr328.clash.service.ClashService
1111import com.github.kr328.clash.service.TunService
12+ import com.github.kr328.clash.service.store.ServiceStore
1213import com.github.kr328.clash.service.util.sendBroadcastSelf
1314
14- fun Context.runClashService (): Intent ? {
15- if (! UiStore (this ).enableClashService)
15+ fun Context.runClashService (manual : Boolean = false ): Intent ? {
16+ if (! ServiceStore (this ).enableClashService && ! manual )
1617 return null ;
1718
1819 val startTun = UiStore (this ).enableVpn
@@ -27,16 +28,17 @@ fun Context.runClashService(): Intent? {
2728 startForegroundServiceCompat(ClashService ::class .intent)
2829 }
2930
31+ ServiceStore (this ).enableClashService = true
32+
3033 return null
3134}
3235
3336fun Context.startClashService (): Intent ? {
34- UiStore (this ).enableClashService = true
35- return runClashService()
37+ return runClashService(manual = true )
3638}
3739
3840fun Context.stopClashService () {
39- UiStore (this ).enableClashService = false
41+ ServiceStore (this ).enableClashService = false
4042 sendBroadcastSelf(Intent (Intents .ACTION_CLASH_REQUEST_STOP ))
4143}
4244
Original file line number Diff line number Diff line change @@ -14,11 +14,6 @@ class UiStore(context: Context) {
1414 .asStoreProvider()
1515 )
1616
17- var enableClashService: Boolean by store.boolean(
18- key = " enable_clash_service" ,
19- defaultValue = false
20- )
21-
2217 var enableVpn: Boolean by store.boolean(
2318 key = " enable_vpn" ,
2419 defaultValue = true
Original file line number Diff line number Diff line change 11package com.github.kr328.clash.service.clash.module
22
33import android.app.Service
4+ import android.os.Build
45import com.github.kr328.clash.common.constants.Intents
56import com.github.kr328.clash.common.log.Log
7+ import com.github.kr328.clash.service.TunService
68
79class CloseModule (service : Service ) : Module<CloseModule.RequestClose>(service) {
810 object RequestClose
911
12+ fun isAlwaysOn (service : Service ): Boolean {
13+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && service is TunService && service.isAlwaysOn
14+ }
15+
1016 override suspend fun run () {
1117 val broadcasts = receiveBroadcast {
1218 addAction(Intents .ACTION_CLASH_REQUEST_STOP )
1319 }
1420
1521 broadcasts.receive()
1622
23+ if (isAlwaysOn(service))
24+ return
25+
1726 Log .d(" User request close" )
1827
1928 return enqueueEvent(RequestClose )
Original file line number Diff line number Diff line change @@ -14,6 +14,11 @@ class ServiceStore(context: Context) {
1414 .asStoreProvider()
1515 )
1616
17+ var enableClashService: Boolean by store.boolean(
18+ key = " enable_clash_service" ,
19+ defaultValue = false
20+ )
21+
1722 var activeProfile: UUID ? by store.typedString(
1823 key = " active_profile" ,
1924 from = { if (it.isBlank()) null else UUID .fromString(it) },
You can’t perform that action at this time.
0 commit comments