Skip to content

Commit fa70f66

Browse files
committed
app: Fix always-on-vpn for android 10+
1 parent 0a7ddb1 commit fa70f66

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

  • app/src/main/java/com/github/kr328/clash/util
  • service/src/main/java/com/github/kr328/clash/service/clash/module

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ fun Context.runClashService(): Intent? {
2626
} else {
2727
startForegroundServiceCompat(ClashService::class.intent)
2828
}
29+
UiStore(this).enableClashService = true
2930

3031
return null
3132
}
3233

3334
fun Context.startClashService(): Intent? {
34-
UiStore(this).enableClashService = true
3535
return runClashService()
3636
}
3737

3838
fun Context.stopClashService() {
39-
UiStore(this).enableClashService = false
4039
sendBroadcastSelf(Intent(Intents.ACTION_CLASH_REQUEST_STOP))
4140
}
4241

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)

0 commit comments

Comments
 (0)