Skip to content

Commit 248364b

Browse files
committed
app: Fix always restarting service
Signed-off-by: hamjin <jinham@qq.com>
1 parent f966120 commit 248364b

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

app/src/main/java/com/github/kr328/clash/RestartReceiver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import android.content.BroadcastReceiver
44
import android.content.Context
55
import android.content.Intent
66
import com.github.kr328.clash.service.StatusProvider
7-
import com.github.kr328.clash.util.startClashService
7+
import com.github.kr328.clash.util.restartClashService
88

99
class RestartReceiver : BroadcastReceiver() {
1010
override fun onReceive(context: Context, intent: Intent) {
1111
when (intent.action) {
1212
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MY_PACKAGE_REPLACED -> {
1313
if (StatusProvider.shouldStartClashOnBoot)
14-
context.startClashService()
14+
context.restartClashService()
1515
}
1616
}
1717
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import com.github.kr328.clash.service.ClashService
1111
import com.github.kr328.clash.service.TunService
1212
import com.github.kr328.clash.service.util.sendBroadcastSelf
1313

14-
fun Context.startClashService(): Intent? {
14+
fun Context.runClashService(): Intent? {
15+
if (!UiStore(this).enableClashService)
16+
return null;
17+
1518
val startTun = UiStore(this).enableVpn
1619

1720
if (startTun) {
@@ -27,6 +30,16 @@ fun Context.startClashService(): Intent? {
2730
return null
2831
}
2932

33+
fun Context.startClashService(): Intent? {
34+
UiStore(this).enableClashService = true
35+
return runClashService()
36+
}
37+
3038
fun Context.stopClashService() {
39+
UiStore(this).enableClashService = false
3140
sendBroadcastSelf(Intent(Intents.ACTION_CLASH_REQUEST_STOP))
32-
}
41+
}
42+
43+
fun Context.restartClashService(): Intent? {
44+
return runClashService();
45+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ 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+
1722
var enableVpn: Boolean by store.boolean(
1823
key = "enable_vpn",
1924
defaultValue = true

0 commit comments

Comments
 (0)