add restore selected proxy

This commit is contained in:
Kr328 committed 2020-04-08 01:57:50 +08:00
1 parent a1847dc6f2
commit 198daaf720
6 files changed
+25 -42

No files matched your search

@@ -44,11 +44,6 @@ class ClashService : BaseService() {
broadcastProfileLoaded()
}
}
onEmpty {
reason = "No profile selected"
stopSelf()
}
}
runtime.install(CloseModule()) {
onClose {
@@ -59,15 +59,6 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
broadcastProfileLoaded()
}
}
onEmpty {
launch {
reason = "No selected profile"
stopSelf()
TunModule.requestStop()
}
}
}
runtime.install(CloseModule()) {
onClose {
@@ -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() {
@@ -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<String>
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)
}
}
@@ -16,7 +16,7 @@ import com.github.kr328.clash.service.data.ClashDatabase
class StaticNotificationModule(private val service: Service) : Module() {
override val receiveBroadcasts: Set<String>
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)
}
@@ -14,5 +14,5 @@ interface ClashProfileProxyDao {
suspend fun querySelectedForProfile(id: Long): List<ClashProfileProxyEntity>
@Query("DELETE FROM profile_select_proxies WHERE profile_id = :id AND proxy in (:selected)")
suspend fun removeSelectedForProfile(id: Int, selected: List<String>)
suspend fun removeSelectedForProfile(id: Long, selected: List<String>)
}