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.
This commit is contained in:
parent
20cc2c09b0
commit
9d9b7bebca
|
@ -17,7 +17,7 @@ android {
|
||||||
|
|
||||||
val versionMajor = 2
|
val versionMajor = 2
|
||||||
val versionMinor = 4
|
val versionMinor = 4
|
||||||
val versionPatch = 1
|
val versionPatch = 2
|
||||||
val versionBuild = 0
|
val versionBuild = 0
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import android.content.Intent
|
||||||
import com.google.firebase.crashlytics.ktx.crashlytics
|
import com.google.firebase.crashlytics.ktx.crashlytics
|
||||||
import com.google.firebase.ktx.Firebase
|
import com.google.firebase.ktx.Firebase
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
object ServiceManager {
|
object ServiceManager {
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
|
@ -35,7 +36,14 @@ object ServiceManager {
|
||||||
intent.component?.javaClass
|
intent.component?.javaClass
|
||||||
try {
|
try {
|
||||||
when(action) {
|
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)
|
Action.STOP -> context.startService(intent)
|
||||||
}
|
}
|
||||||
} catch (e : Exception) {
|
} catch (e : Exception) {
|
||||||
|
|
|
@ -47,6 +47,7 @@ class WireGuardTunnelService : ForegroundService() {
|
||||||
val tunnelConfig = TunnelConfig.from(tunnelConfigString)
|
val tunnelConfig = TunnelConfig.from(tunnelConfigString)
|
||||||
tunnelName = tunnelConfig.name
|
tunnelName = tunnelConfig.name
|
||||||
vpnService.startTunnel(tunnelConfig)
|
vpnService.startTunnel(tunnelConfig)
|
||||||
|
launchVpnStartingNotification()
|
||||||
} catch (e : Exception) {
|
} catch (e : Exception) {
|
||||||
Timber.e("Problem starting tunnel: ${e.message}")
|
Timber.e("Problem starting tunnel: ${e.message}")
|
||||||
stopService(extras)
|
stopService(extras)
|
||||||
|
@ -60,6 +61,7 @@ class WireGuardTunnelService : ForegroundService() {
|
||||||
val tunnelConfig = TunnelConfig.from(setting.defaultTunnel!!)
|
val tunnelConfig = TunnelConfig.from(setting.defaultTunnel!!)
|
||||||
tunnelName = tunnelConfig.name
|
tunnelName = tunnelConfig.name
|
||||||
vpnService.startTunnel(tunnelConfig)
|
vpnService.startTunnel(tunnelConfig)
|
||||||
|
launchVpnStartingNotification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,6 +119,18 @@ class WireGuardTunnelService : ForegroundService() {
|
||||||
super.startForeground(foregroundId, notification)
|
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) {
|
private fun launchVpnConnectionFailedNotification(message : String) {
|
||||||
val notification = notificationService.createNotification(
|
val notification = notificationService.createNotification(
|
||||||
channelId = getString(R.string.vpn_channel_id),
|
channelId = getString(R.string.vpn_channel_id),
|
||||||
|
|
|
@ -89,4 +89,6 @@
|
||||||
<string name="hint_search_packages">Search packages</string>
|
<string name="hint_search_packages">Search packages</string>
|
||||||
<string name="clear_icon">Clear Icon</string>
|
<string name="clear_icon">Clear Icon</string>
|
||||||
<string name="search_icon">Search Icon</string>
|
<string name="search_icon">Search Icon</string>
|
||||||
|
<string name="attempt_connection">Attempting connection..</string>
|
||||||
|
<string name="vpn_starting">VPN Starting</string>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in New Issue