Skip to content

Commit 9dbb628

Browse files
committed
Fix foreground not starting in time
1 parent 2b7a4c6 commit 9dbb628

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt

+26-24
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import arrow.atomic.AtomicInt
1212
import co.touchlab.kermit.Logger
1313
import java.io.File
1414
import kotlinx.coroutines.flow.filter
15-
import kotlinx.coroutines.flow.first
15+
import kotlinx.coroutines.flow.filterIsInstance
1616
import kotlinx.coroutines.launch
1717
import kotlinx.coroutines.runBlocking
1818
import net.mullvad.mullvadvpn.lib.common.constant.KEY_CONNECT_ACTION
@@ -90,6 +90,17 @@ class MullvadVpnService : TalpidVpnService() {
9090

9191
Logger.i("Start management service")
9292
managementService.start()
93+
94+
lifecycleScope.launch {
95+
managementService.tunnelState
96+
.filterIsInstance<TunnelState.Error>()
97+
.filter { !it.errorState.isBlocking }
98+
.collect {
99+
if (bindCount.get() == 0) {
100+
foregroundNotificationHandler.stopForeground()
101+
}
102+
}
103+
}
93104
}
94105

95106
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -105,15 +116,23 @@ class MullvadVpnService : TalpidVpnService() {
105116
keyguardManager.isKeyguardLocked -> {
106117
Logger.i("Keyguard is locked, ignoring command")
107118
}
119+
108120
intent.isFromSystem() || intent?.action == KEY_CONNECT_ACTION -> {
109-
// Only show on foreground if we have permission
110-
if (prepare(this) == null) {
111-
foregroundNotificationHandler.startForeground()
112-
}
121+
foregroundNotificationHandler.startForeground()
113122
lifecycleScope.launch { connectionProxy.connectWithoutPermissionCheck() }
114123
}
124+
115125
intent?.action == KEY_DISCONNECT_ACTION -> {
126+
// Tile might of launched the service with the expectancy to be in foreground, thus
127+
// must quickly go into foreground to please the android system requirements.
128+
foregroundNotificationHandler.startForeground()
116129
lifecycleScope.launch { connectionProxy.disconnect() }
130+
131+
// If disconnect intent is received and no one is using this service, simply stop
132+
// foreground.
133+
if (bindCount.get() == 0) {
134+
foregroundNotificationHandler.stopForeground()
135+
}
117136
}
118137
}
119138

@@ -180,25 +199,6 @@ class MullvadVpnService : TalpidVpnService() {
180199
foregroundNotificationHandler.stopForeground()
181200
}
182201

183-
if (count == 0) {
184-
Logger.i("No one bound to the service, stopSelf()")
185-
lifecycleScope.launch {
186-
Logger.i("Waiting for disconnected state")
187-
// TODO This needs reworking, we should not wait for the disconnected state, what we
188-
// want is the notification of disconnected to go out before we start shutting down
189-
connectionProxy.tunnelState
190-
.filter {
191-
it is TunnelState.Disconnected ||
192-
(it is TunnelState.Error && !it.errorState.isBlocking)
193-
}
194-
.first()
195-
196-
if (bindCount.get() == 0) {
197-
Logger.i("Stopping service")
198-
stopSelf()
199-
}
200-
}
201-
}
202202
return true
203203
}
204204

@@ -209,8 +209,10 @@ class MullvadVpnService : TalpidVpnService() {
209209

210210
Logger.i("Shutdown MullvadDaemon")
211211
MullvadDaemon.shutdown()
212+
212213
Logger.i("Enter Idle")
213214
managementService.enterIdle()
215+
214216
Logger.i("Shutdown complete")
215217
super.onDestroy()
216218
}

0 commit comments

Comments
 (0)