@@ -12,7 +12,7 @@ import arrow.atomic.AtomicInt
12
12
import co.touchlab.kermit.Logger
13
13
import java.io.File
14
14
import kotlinx.coroutines.flow.filter
15
- import kotlinx.coroutines.flow.first
15
+ import kotlinx.coroutines.flow.filterIsInstance
16
16
import kotlinx.coroutines.launch
17
17
import kotlinx.coroutines.runBlocking
18
18
import net.mullvad.mullvadvpn.lib.common.constant.KEY_CONNECT_ACTION
@@ -90,6 +90,17 @@ class MullvadVpnService : TalpidVpnService() {
90
90
91
91
Logger .i(" Start management service" )
92
92
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
+ }
93
104
}
94
105
95
106
override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
@@ -105,15 +116,23 @@ class MullvadVpnService : TalpidVpnService() {
105
116
keyguardManager.isKeyguardLocked -> {
106
117
Logger .i(" Keyguard is locked, ignoring command" )
107
118
}
119
+
108
120
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()
113
122
lifecycleScope.launch { connectionProxy.connectWithoutPermissionCheck() }
114
123
}
124
+
115
125
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()
116
129
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
+ }
117
136
}
118
137
}
119
138
@@ -180,25 +199,6 @@ class MullvadVpnService : TalpidVpnService() {
180
199
foregroundNotificationHandler.stopForeground()
181
200
}
182
201
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
- }
202
202
return true
203
203
}
204
204
@@ -209,8 +209,10 @@ class MullvadVpnService : TalpidVpnService() {
209
209
210
210
Logger .i(" Shutdown MullvadDaemon" )
211
211
MullvadDaemon .shutdown()
212
+
212
213
Logger .i(" Enter Idle" )
213
214
managementService.enterIdle()
215
+
214
216
Logger .i(" Shutdown complete" )
215
217
super .onDestroy()
216
218
}
0 commit comments