Skip to content

Commit

Permalink
fix: tile bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneschepke committed Aug 3, 2024
1 parent b99c2ff commit 5b0bb2a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.zaneschepke.wireguardautotunnel

import android.app.Application
import android.content.ComponentName
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.service.quicksettings.TileService
import com.zaneschepke.wireguardautotunnel.service.tile.AutoTunnelControlTile
import com.zaneschepke.wireguardautotunnel.service.tile.TunnelControlTile
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber
Expand All @@ -25,10 +29,26 @@ class WireGuardAutoTunnel : Application() {
} else {
Timber.plant(ReleaseTree())
}
requestTunnelTileServiceStateUpdate()
requestAutoTunnelTileServiceUpdate()
}

companion object {
lateinit var instance: WireGuardAutoTunnel
private set
}

private fun requestTunnelTileServiceStateUpdate() {
TileService.requestListeningState(
instance,
ComponentName(instance, TunnelControlTile::class.java),
)
}

private fun requestAutoTunnelTileServiceUpdate() {
TileService.requestListeningState(
instance,
ComponentName(instance, AutoTunnelControlTile::class.java),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ interface AppDataRepository {

suspend fun getStartTunnelConfig(): TunnelConfig?

suspend fun toggleWatcherServicePause()

val settings: SettingsRepository
val tunnels: TunnelConfigRepository
val appState: AppStateRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,4 @@ constructor(
getPrimaryOrFirstTunnel()
}
}

override suspend fun toggleWatcherServicePause() {
val settings = settings.getSettings()
if (settings.isAutoTunnelEnabled) {
val pauseAutoTunnel = !settings.isAutoTunnelPaused
this.settings.save(
settings.copy(
isAutoTunnelPaused = pauseAutoTunnel,
),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -38,21 +37,25 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {

applicationScope.launch {
appDataRepository.settings.getSettingsFlow().collect {
when (it.isAutoTunnelEnabled) {
true -> {
if (it.isAutoTunnelPaused) {
setInactive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.paused))
} else {
setActive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.active))
kotlin.runCatching {
when (it.isAutoTunnelEnabled) {
true -> {
if (it.isAutoTunnelPaused) {
setInactive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.paused))
} else {
setActive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.active))
}
}
}

false -> {
setTileDescription(this@AutoTunnelControlTile.getString(R.string.disabled))
setUnavailable()
false -> {
setTileDescription(this@AutoTunnelControlTile.getString(R.string.disabled))
setUnavailable()
}
}
}.onFailure {
Timber.e(it)
}
}
}
Expand All @@ -76,12 +79,20 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
super.onClick()
unlockAndRun {
lifecycleScope.launch {
try {
appDataRepository.toggleWatcherServicePause()
} catch (e: Exception) {
Timber.e(e.message)
} finally {
cancel()
kotlin.runCatching {
val settings = appDataRepository.settings.getSettings()
if (settings.isAutoTunnelPaused) {
return@launch appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = false,
),
)
}
appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = true,
),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TunnelControlTile : TileService(), LifecycleOwner {

override fun onCreate() {
super.onCreate()
Timber.d("onCreate for tile service")
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)

applicationScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ constructor(
)
}

suspend fun onTunnelStart(tunnelConfig: TunnelConfig) = viewModelScope.launch {
fun onTunnelStart(tunnelConfig: TunnelConfig) = viewModelScope.launch {
Timber.i("Starting tunnel ${tunnelConfig.name}")
tunnelService.startTunnel(tunnelConfig).onSuccess {
appDataRepository.appState.setTunnelRunningFromManualStart(tunnelConfig.id)
Expand Down

0 comments on commit 5b0bb2a

Please sign in to comment.