Skip to content

Commit d63f7ab

Browse files
committed
app: Fix always-on-vpn for android 10+
Signed-off-by: hamjin <jinham@qq.com>
1 parent 248364b commit d63f7ab

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

app/src/main/java/com/github/kr328/clash/util/Clash.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import com.github.kr328.clash.common.util.intent
99
import com.github.kr328.clash.design.store.UiStore
1010
import com.github.kr328.clash.service.ClashService
1111
import com.github.kr328.clash.service.TunService
12+
import com.github.kr328.clash.service.store.ServiceStore
1213
import 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

3336
fun Context.startClashService(): Intent? {
34-
UiStore(this).enableClashService = true
35-
return runClashService()
37+
return runClashService(manual = true)
3638
}
3739

3840
fun Context.stopClashService() {
39-
UiStore(this).enableClashService = false
41+
ServiceStore(this).enableClashService = false
4042
sendBroadcastSelf(Intent(Intents.ACTION_CLASH_REQUEST_STOP))
4143
}
4244

design/src/main/java/com/github/kr328/clash/design/store/UiStore.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff 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

service/src/main/java/com/github/kr328/clash/service/clash/module/CloseModule.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package com.github.kr328.clash.service.clash.module
22

33
import android.app.Service
4+
import android.os.Build
45
import com.github.kr328.clash.common.constants.Intents
56
import com.github.kr328.clash.common.log.Log
7+
import com.github.kr328.clash.service.TunService
68

79
class 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)

service/src/main/java/com/github/kr328/clash/service/store/ServiceStore.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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) },

0 commit comments

Comments
 (0)