From 9d9b7bebca3f39c8c7ff94aaa13d21784dbaa5e7 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Mon, 4 Sep 2023 05:52:15 -0400 Subject: [PATCH] fix: foreground service crash on older devices Fixes a bug where older device take a longer time to launch the foreground service and connect to the VPN. Combined with a delayed launch of foreground notification until VPN connection is confirmed, this would break foreground service,s 5 second notification rule. Fixed by adding a new attempting connection notification to launch on vpn initial connection attempt. --- app/build.gradle.kts | 2 +- .../service/foreground/ServiceManager.kt | 10 +++++++++- .../service/foreground/WireGuardTunnelService.kt | 14 ++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index fda21cf..1bb9ea6 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,7 +17,7 @@ android { val versionMajor = 2 val versionMinor = 4 - val versionPatch = 1 + val versionPatch = 2 val versionBuild = 0 defaultConfig { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceManager.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceManager.kt index 960f0e8..c410643 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceManager.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceManager.kt @@ -9,6 +9,7 @@ import android.content.Intent import com.google.firebase.crashlytics.ktx.crashlytics import com.google.firebase.ktx.Firebase import com.zaneschepke.wireguardautotunnel.R +import timber.log.Timber object ServiceManager { @Suppress("DEPRECATION") @@ -35,7 +36,14 @@ object ServiceManager { intent.component?.javaClass try { when(action) { - Action.START -> context.startForegroundService(intent) + Action.START -> { + try { + context.startForegroundService(intent) + } catch (e : Exception) { + Timber.e("Unable to start service foreground ${e.message}") + context.startService(intent) + } + } Action.STOP -> context.startService(intent) } } catch (e : Exception) { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt index 614e92a..a43d2c8 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt @@ -47,6 +47,7 @@ class WireGuardTunnelService : ForegroundService() { val tunnelConfig = TunnelConfig.from(tunnelConfigString) tunnelName = tunnelConfig.name vpnService.startTunnel(tunnelConfig) + launchVpnStartingNotification() } catch (e : Exception) { Timber.e("Problem starting tunnel: ${e.message}") stopService(extras) @@ -60,6 +61,7 @@ class WireGuardTunnelService : ForegroundService() { val tunnelConfig = TunnelConfig.from(setting.defaultTunnel!!) tunnelName = tunnelConfig.name vpnService.startTunnel(tunnelConfig) + launchVpnStartingNotification() } } } @@ -117,6 +119,18 @@ class WireGuardTunnelService : ForegroundService() { super.startForeground(foregroundId, notification) } + private fun launchVpnStartingNotification() { + val notification = notificationService.createNotification( + channelId = getString(R.string.vpn_channel_id), + channelName = getString(R.string.vpn_channel_name), + title = getString(R.string.vpn_starting), + onGoing = false, + showTimestamp = true, + description = getString(R.string.attempt_connection) + ) + super.startForeground(foregroundId, notification) + } + private fun launchVpnConnectionFailedNotification(message : String) { val notification = notificationService.createNotification( channelId = getString(R.string.vpn_channel_id), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14df420..7a2536e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -89,4 +89,6 @@ Search packages Clear Icon Search Icon + Attempting connection.. + VPN Starting \ No newline at end of file