diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index 883a656..61debc5 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -44,11 +44,6 @@ class ClashService : BaseService() { broadcastProfileLoaded() } } - onEmpty { - reason = "No profile selected" - - stopSelf() - } } runtime.install(CloseModule()) { onClose { diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index aeb8960..a5915e1 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -59,15 +59,6 @@ class TunService : VpnService(), CoroutineScope by MainScope() { broadcastProfileLoaded() } } - onEmpty { - launch { - reason = "No selected profile" - - stopSelf() - - TunModule.requestStop() - } - } } runtime.install(CloseModule()) { onClose { diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt index 77f7863..6888d8c 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt @@ -20,7 +20,7 @@ class DynamicNotificationModule(private val service: Service) : Module() { get() = setOf( Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF, - Intents.INTENT_ACTION_PROFILE_CHANGED + Intents.INTENT_ACTION_PROFILE_LOADED ) private val contentIntent = Intent(Intent.ACTION_MAIN) .addCategory(Intent.CATEGORY_DEFAULT) @@ -50,15 +50,13 @@ class DynamicNotificationModule(private val service: Service) : Module() { enableTicker = true Intent.ACTION_SCREEN_OFF -> enableTicker = false - Intents.INTENT_ACTION_PROFILE_CHANGED -> + Intents.INTENT_ACTION_PROFILE_LOADED -> reload() } } override suspend fun onStart() { enableTicker = service.getSystemService(PowerManager::class.java).isInteractive - - reload() } override suspend fun onStop() { diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/ReloadModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/ReloadModule.kt index 89e9137..5eeaed9 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/ReloadModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/ReloadModule.kt @@ -4,17 +4,17 @@ import android.content.Context import android.content.Intent import com.github.kr328.clash.common.ids.Intents import com.github.kr328.clash.core.Clash +import com.github.kr328.clash.service.ServiceStatusProvider import com.github.kr328.clash.service.data.ClashDatabase import com.github.kr328.clash.service.util.resolveBase import com.github.kr328.clash.service.util.resolveProfile import kotlinx.coroutines.sync.Mutex -import java.lang.Exception +import java.lang.NullPointerException class ReloadModule(private val context: Context) : Module() { override val receiveBroadcasts: Set get() = setOf(Intents.INTENT_ACTION_NETWORK_CHANGED, Intents.INTENT_ACTION_PROFILE_CHANGED) private val reloadMutex = Mutex() - private var emptyCallback: () -> Unit = {} private var loadedCallback: (Exception?) -> Unit = {} override suspend fun onStart() { @@ -34,29 +34,32 @@ class ReloadModule(private val context: Context) : Module() { reloadMutex.unlock() } - fun onEmpty(callback: () -> Unit) { - emptyCallback = callback - } fun onLoaded(callback: (Exception?) -> Unit) { loadedCallback = callback } private suspend fun reload() { - val database = ClashDatabase.getInstance(context).openClashProfileDao() - - val id = database.queryActiveProfile()?.id - if (id == null) { - emptyCallback() - reloadMutex.unlock() - return - } - try { - Clash.loadProfile(resolveProfile(id), resolveBase(id)) + val database = ClashDatabase.getInstance(context) + val profileDao = database.openClashProfileDao() + val proxyDao = database.openClashProfileProxyDao() + + val active = profileDao.queryActiveProfile() + ?: throw NullPointerException("No profile selected") + + Clash.loadProfile(resolveProfile(active.id), resolveBase(active.id)).await() + + val remove = proxyDao.querySelectedForProfile(active.id) + .filterNot { Clash.setSelectedProxy(it.proxy, it.selected) } + .map { it.selected } + + proxyDao.removeSelectedForProfile(active.id, remove) + + ServiceStatusProvider.currentProfile = active.name + loadedCallback(null) - } - catch (e: Exception) { + } catch (e: Exception) { loadedCallback(e) } } diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt index e87c5ff..c835b3f 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt @@ -16,7 +16,7 @@ import com.github.kr328.clash.service.data.ClashDatabase class StaticNotificationModule(private val service: Service) : Module() { override val receiveBroadcasts: Set - get() = setOf(Intents.INTENT_ACTION_PROFILE_CHANGED) + get() = setOf(Intents.INTENT_ACTION_PROFILE_LOADED) private val contentIntent = Intent(Intent.ACTION_MAIN) .addCategory(Intent.CATEGORY_DEFAULT) .addCategory(Intent.CATEGORY_LAUNCHER) @@ -40,16 +40,12 @@ class StaticNotificationModule(private val service: Service) : Module() { override suspend fun onBroadcastReceived(intent: Intent) { when (intent.action) { - Intents.INTENT_ACTION_PROFILE_CHANGED -> { + Intents.INTENT_ACTION_PROFILE_LOADED -> { update() } } } - override suspend fun onStart() { - update() - } - override suspend fun onStop() { service.stopForeground(true) } diff --git a/service/src/main/java/com/github/kr328/clash/service/data/ClashProfileProxyDao.kt b/service/src/main/java/com/github/kr328/clash/service/data/ClashProfileProxyDao.kt index 971cb1d..8b23def 100644 --- a/service/src/main/java/com/github/kr328/clash/service/data/ClashProfileProxyDao.kt +++ b/service/src/main/java/com/github/kr328/clash/service/data/ClashProfileProxyDao.kt @@ -14,5 +14,5 @@ interface ClashProfileProxyDao { suspend fun querySelectedForProfile(id: Long): List @Query("DELETE FROM profile_select_proxies WHERE profile_id = :id AND proxy in (:selected)") - suspend fun removeSelectedForProfile(id: Int, selected: List) + suspend fun removeSelectedForProfile(id: Long, selected: List) } \ No newline at end of file