Skip to content

Commit

Permalink
feat: user logger toggle control
Browse files Browse the repository at this point in the history
Allow users to toggle on and off local logger. This will hopefully help address #437
Allow shortcuts activation on AndroidTV
closes #451
  • Loading branch information
zaneschepke committed Nov 23, 2024
1 parent b2938c3 commit f9ef308
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import com.zaneschepke.logcatter.LogReader
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
Expand All @@ -32,6 +33,9 @@ class WireGuardAutoTunnel : Application() {
@Inject
lateinit var logReader: LogReader

@Inject
lateinit var appStateRepository: AppStateRepository

@Inject
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
Expand All @@ -54,7 +58,10 @@ class WireGuardAutoTunnel : Application() {
}
if (!isRunningOnTv()) {
applicationScope.launch(ioDispatcher) {
logReader.start()
if (appStateRepository.isLocalLogsEnabled()) {
Timber.d("Starting logger")
logReader.start()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DataStoreManager(
val currentSSID = stringPreferencesKey("CURRENT_SSID")
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
val isLocalLogsEnabled = booleanPreferencesKey("LOCAL_LOGS_ENABLED")
val theme = stringPreferencesKey("THEME")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ data class GeneralState(
val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
val isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED,
val isLocalLogsEnabled: Boolean = IS_LOGS_ENABLED_DEFAULT,
val theme: Theme = Theme.AUTOMATIC,
) {
companion object {
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false
const val PIN_LOCK_ENABLED_DEFAULT = false
const val IS_TUNNEL_STATS_EXPANDED = false
const val IS_LOGS_ENABLED_DEFAULT = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ interface AppStateRepository {

suspend fun getTheme(): Theme

suspend fun isLocalLogsEnabled(): Boolean

suspend fun setLocalLogsEnabled(enabled: Boolean)

val generalStateFlow: Flow<GeneralState>
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class DataStoreAppStateRepository(
} ?: Theme.AUTOMATIC
}

override suspend fun isLocalLogsEnabled(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.isLocalLogsEnabled) ?: GeneralState.IS_LOGS_ENABLED_DEFAULT
}

override suspend fun setLocalLogsEnabled(enabled: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.isLocalLogsEnabled, enabled)
}

override val generalStateFlow: Flow<GeneralState> =
dataStoreManager.preferencesFlow.map { prefs ->
prefs?.let { pref ->
Expand All @@ -84,6 +92,7 @@ class DataStoreAppStateRepository(
pref[DataStoreManager.pinLockEnabled]
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
isLocalLogsEnabled = pref[DataStoreManager.isLocalLogsEnabled] ?: GeneralState.IS_LOGS_ENABLED_DEFAULT,
theme = getTheme(),
)
} catch (e: IllegalArgumentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ constructor(
vpnState.copy(
tunnelConfig = tunnels.firstOrNull { it.id == vpnState.tunnelConfig?.id },
)
}.stateIn(applicationScope, SharingStarted.Lazily, VpnState())
}.stateIn(applicationScope, SharingStarted.Eagerly, VpnState())

private var statsJob: Job? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.util.RootShell
import com.zaneschepke.logcatter.LogReader
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
Expand All @@ -25,6 +26,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
Expand All @@ -41,6 +43,7 @@ constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@AppShell private val rootShell: Provider<RootShell>,
private val serviceManager: ServiceManager,
private val logReader: LogReader,
) : ViewModel() {

val uiState =
Expand All @@ -64,11 +67,14 @@ constructor(
AppUiState(),
)

private val _isAppReady = MutableStateFlow<Boolean>(false)
private val _isAppReady = MutableStateFlow(false)
val isAppReady = _isAppReady.asStateFlow()

private val _configurationChange = MutableStateFlow(false)
val configurationChange = _configurationChange.asStateFlow()

init {
viewModelScope.launch {
viewModelScope.launch(ioDispatcher) {
initPin()
initAutoTunnel()
initTunnel()
Expand All @@ -84,7 +90,6 @@ constructor(
}

private suspend fun initTunnel() {
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
val activeTunnels = appDataRepository.tunnels.getActive()
if (activeTunnels.isNotEmpty() &&
tunnelService.get().getState() == TunnelState.DOWN
Expand Down Expand Up @@ -116,6 +121,22 @@ constructor(
appDataRepository.appState.setLocationDisclosureShown(true)
}

fun onToggleLocalLogging() = viewModelScope.launch(ioDispatcher) {
with(uiState.value.generalState) {
val toggledOn = !isLocalLogsEnabled
appDataRepository.appState.setLocalLogsEnabled(toggledOn)
if (!toggledOn) onLoggerStop()
_configurationChange.update {
true
}
}
}

private suspend fun onLoggerStop() {
logReader.stop()
logReader.deleteAndClearLogs()
}

fun onToggleAlwaysOnVPN() = viewModelScope.launch {
with(uiState.value.settings) {
appDataRepository.settings.save(
Expand Down
Loading

0 comments on commit f9ef308

Please sign in to comment.