From cca7ec81e19334a6cc2b83edf3f33be777501023 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Tue, 21 Jan 2020 20:27:05 +0800 Subject: [PATCH] service refactored --- .../kr328/clash/service/ClashProfileManager.kt | 15 +++++++++++++++ .../com/github/kr328/clash/service/Intents.kt | 2 ++ 2 files changed, 17 insertions(+) diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashProfileManager.kt b/service/src/main/java/com/github/kr328/clash/service/ClashProfileManager.kt index be9d888..8e56bf1 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashProfileManager.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashProfileManager.kt @@ -1,6 +1,7 @@ package com.github.kr328.clash.service import android.content.Context +import android.content.Intent import android.net.Uri import com.github.kr328.clash.core.Clash import com.github.kr328.clash.service.data.ClashDatabase @@ -8,6 +9,7 @@ import com.github.kr328.clash.service.data.ClashProfileEntity import com.github.kr328.clash.service.ipc.ParcelableCompletedFuture import com.github.kr328.clash.service.util.DefaultThreadPool import com.github.kr328.clash.service.util.FileUtils +import com.github.kr328.clash.service.util.sendBroadcastSelf class ClashProfileManager(private val context: Context, private val database: ClashDatabase) : IClashProfileManager.Stub() { @@ -29,6 +31,8 @@ class ClashProfileManager(private val context: Context, private val database: Cl result.complete(null) database.openClashProfileDao().touchProfile(id) + + sendChangedBroadcast() }, onFailure = { result.completeExceptionally(it) @@ -62,6 +66,8 @@ class ClashProfileManager(private val context: Context, private val database: Cl ) ) + sendChangedBroadcast() + result.complete(null) }, onFailure = { @@ -113,4 +119,13 @@ class ClashProfileManager(private val context: Context, private val database: Cl } } } + + private fun sendChangedBroadcast() { + val active = database.openClashProfileDao().queryActiveProfile() + + context.sendBroadcastSelf( + Intent(Intents.INTENT_ACTION_PROFILE_CHANGED) + .putExtra(Intents.INTENT_ACTION_PROFILE_ACTIVE_NAME, active?.name) + ) + } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/Intents.kt b/service/src/main/java/com/github/kr328/clash/service/Intents.kt index 8d3d037..d89fe18 100644 --- a/service/src/main/java/com/github/kr328/clash/service/Intents.kt +++ b/service/src/main/java/com/github/kr328/clash/service/Intents.kt @@ -12,4 +12,6 @@ object Intents { const val INTENT_ACTION_CLASH_STOP_REASON = "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.clash.stop.reason" + const val INTENT_ACTION_PROFILE_ACTIVE_NAME = + "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile.active.name" } \ No newline at end of file