Skip to content

Commit

Permalink
Clarify logic and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed May 29, 2024
1 parent b9f733f commit 902883d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {

override fun onCreate() {
super.onCreate()
Log.d(TAG, "MullvadVpnService: onCreate")

loadKoinModules(listOf(vpnServiceModule, apiEndpointModule))
with(getKoin()) {
Expand Down Expand Up @@ -96,20 +97,24 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(TAG, "onStartCommand (intent=$intent, flags=$flags, startId=$startId)")
Log.d(TAG, "intent action=${intent?.action}")
Log.d(
TAG,
"onStartCommand (intent=$intent, action=${intent?.action}, flags=$flags, startId=$startId)"
)

val startResult = super.onStartCommand(intent, flags, startId)

// Always promote to foreground if connect/disconnect actions are provided to mitigate cases
// where the service would potentially otherwise be too slow running `startForeground`.
Log.d(TAG, "Intent Action: ${intent?.action}")
when {
keyguardManager.isKeyguardLocked -> {
Log.d(TAG, "Keyguard is locked, ignoring command")
}
intent.isFromSystem() || intent?.action == KEY_CONNECT_ACTION -> {
_shouldBeOnForeground.update { true }
// Only show on foreground if we have permission
if (prepare(this) == null) {
_shouldBeOnForeground.update { true }
}
lifecycleScope.launch { connectionProxy.connectWithoutPermissionCheck() }
}
intent?.action == KEY_DISCONNECT_ACTION -> {
Expand Down Expand Up @@ -177,6 +182,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
}

override fun onDestroy() {
Log.d(TAG, "MullvadVpnService: onDestroy")
managementService.stop()

// Shutting down the daemon gracefully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class ForegroundNotificationManager(
scope.launch {
foregroundProvider.shouldBeOnForeground.collect {
if (it) {
Log.d(TAG, "Starting foreground")
notifyForeground(getTunnelStateNotificationOrDefault())
} else {
Log.d(TAG, "Stopping foreground")
vpnService.stopForeground(Service.STOP_FOREGROUND_DETACH)
}
}
Expand All @@ -46,19 +48,20 @@ class ForegroundNotificationManager(
private fun notifyForeground(tunnelStateNotification: Notification.Tunnel) {

val androidNotification = tunnelStateNotification.toNotification(vpnService)
if (VpnService.prepare(vpnService) != null) {
// Got connect/disconnect intent, but we don't have permission to go in foreground.
// tunnel state will return permission and we will eventually get stopped by system.
Log.d(TAG, "Did not start foreground: VPN permission not granted")
return
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (VpnService.prepare(vpnService) == null) {
vpnService.startForeground(
tunnelStateNotificationProvider.notificationId.value,
androidNotification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
)
} else {
// Got connect/disconnect intent, but we don't have permission to go in foreground.
// tunnel state will return permission and we will eventually get stopped by system.
Log.d(TAG, "ForegroundNotification: VPN permission not granted")
return
}
Log.d(TAG, "Starting foreground UPSIDE_DOWN_CAKE")
vpnService.startForeground(
tunnelStateNotificationProvider.notificationId.value,
androidNotification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
)
} else {
vpnService.startForeground(
tunnelStateNotificationProvider.notificationId.value,
Expand Down

0 comments on commit 902883d

Please sign in to comment.