diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 66b872e..ea40639 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,7 +17,7 @@ android { val versionMajor = 1 val versionMinor = 1 - val versionPatch = 3 + val versionPatch = 4 val versionBuild = 0 defaultConfig { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ForegroundService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ForegroundService.kt index b1ffe6c..d6c2bec 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ForegroundService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ForegroundService.kt @@ -45,7 +45,6 @@ open class ForegroundService : Service() { if (isServiceStarted) return Timber.d("Starting ${this.javaClass.simpleName}") isServiceStarted = true - ServiceTracker.setServiceState(this, ServiceState.STARTED, this.javaClass) } protected open fun stopService(extras : Bundle?) { @@ -57,6 +56,5 @@ open class ForegroundService : Service() { Timber.d("Service stopped without being started: ${e.message}") } isServiceStarted = false - ServiceTracker.setServiceState(this, ServiceState.STOPPED, this.javaClass) } } \ No newline at end of file diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt index 8dadca9..9309a8a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt @@ -1,29 +1,25 @@ package com.zaneschepke.wireguardautotunnel.service.foreground +import android.app.ActivityManager import android.app.Application import android.app.Service import android.content.Context +import android.content.Context.ACTIVITY_SERVICE import android.content.Intent import android.content.SharedPreferences import com.zaneschepke.wireguardautotunnel.R object ServiceTracker { - fun setServiceState(context: Context, state: ServiceState, cls : Class) { - val sharedPrefs = getPreferences(context) - sharedPrefs.edit().let { - it.putString(cls.simpleName, state.name) - it.apply() - } - } + @Suppress("DEPRECATION") + private // Deprecated for third party Services. + fun Context.isServiceRunning(service: Class) = + (getSystemService(ACTIVITY_SERVICE) as ActivityManager) + .getRunningServices(Integer.MAX_VALUE) + .any { it.service.className == service.name } - private fun getServiceState(context: Context, cls : Class): ServiceState { - val sharedPrefs = getPreferences(context) - val value = sharedPrefs.getString(cls.simpleName, ServiceState.STOPPED.name) - return ServiceState.valueOf(value!!) - } - - private fun getPreferences(context: Context): SharedPreferences { - return context.getSharedPreferences(context.resources.getString(R.string.foreground_file), 0) + fun getServiceState(context: Context, cls : Class): ServiceState { + val isServiceRunning = context.isServiceRunning(cls) + return if(isServiceRunning) ServiceState.STARTED else ServiceState.STOPPED } fun actionOnService(action: Action, application: Application, cls : Class, extras : Map? = null) { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt index 381dfe4..9cd5f6b 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt @@ -131,12 +131,12 @@ class WireGuardConnectivityWatcherService : ForegroundService() { if(!settings.isNullOrEmpty()) { setting = settings[0] } + watchForWifiConnectivityChanges() if(setting.isTunnelOnMobileDataEnabled) { GlobalScope.launch { watchForMobileDataConnectivityChanges() } } - watchForWifiConnectivityChanges() } } @@ -171,16 +171,18 @@ class WireGuardConnectivityWatcherService : ForegroundService() { isWifiConnected = true } is NetworkStatus.CapabilitiesChanged -> { + Timber.d("Wifi capabilities changed") isWifiConnected = true if (!connecting && !disconnecting) { + Timber.d("Not connect and not disconnecting") val ssid = wifiService.getNetworkName(it.networkCapabilities); Timber.d("SSID: $ssid") if ((setting.trustedNetworkSSIDs?.contains(ssid) == false) && vpnService.getState() == Tunnel.State.DOWN) { Timber.d("Starting VPN Tunnel for untrusted network: $ssid") startVPN() - } else if (!disconnecting && vpnService.getState() == Tunnel.State.UP && (setting.trustedNetworkSSIDs?.contains( + } else if (!disconnecting && vpnService.getState() == Tunnel.State.UP && setting.trustedNetworkSSIDs.contains( ssid - ) == true) + ) ) { Timber.d("Stopping VPN Tunnel for trusted network with ssid: $ssid") stopVPN() @@ -191,7 +193,10 @@ class WireGuardConnectivityWatcherService : ForegroundService() { isWifiConnected = false Timber.d("Lost Wi-Fi connection") if(setting.isTunnelOnMobileDataEnabled && vpnService.getState() == Tunnel.State.DOWN - && isMobileDataConnected) startVPN() + && isMobileDataConnected){ + Timber.d("Wifi not available so starting vpn for mobile data") + startVPN() + } } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt index da9ed9d..3fa0e45 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt @@ -11,6 +11,7 @@ import com.wireguard.config.Config import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.repository.Repository import com.zaneschepke.wireguardautotunnel.service.foreground.Action +import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceState import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceTracker import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService @@ -23,6 +24,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject @@ -48,11 +50,26 @@ class MainViewModel @Inject constructor(private val application : Application, viewModelScope.launch { settingsRepo.itemFlow.collect { val settings = it.first() + validateWatcherServiceState(settings) _settings.emit(settings) } } } + private fun validateWatcherServiceState(settings: Settings) { + val watcherState = ServiceTracker.getServiceState(application, WireGuardConnectivityWatcherService::class.java) + if(settings.isAutoTunnelEnabled && watcherState == ServiceState.STOPPED && settings.defaultTunnel != null) { + startWatcherService(settings.defaultTunnel!!) + } + } + + private fun startWatcherService(tunnel : String) { + ServiceTracker.actionOnService( + Action.START, application, + WireGuardConnectivityWatcherService::class.java, + mapOf(application.resources.getString(R.string.tunnel_extras_key) to tunnel)) + } + fun onDelete(tunnel : TunnelConfig) { viewModelScope.launch { if(tunnelRepo.count() == 1L) {