@@ -12,11 +12,8 @@ import arrow.atomic.AtomicInt
12
12
import co.touchlab.kermit.Logger
13
13
import java.io.File
14
14
import kotlinx.coroutines.Dispatchers
15
- import kotlinx.coroutines.flow.MutableStateFlow
16
- import kotlinx.coroutines.flow.StateFlow
17
15
import kotlinx.coroutines.flow.filter
18
16
import kotlinx.coroutines.flow.first
19
- import kotlinx.coroutines.flow.update
20
17
import kotlinx.coroutines.launch
21
18
import kotlinx.coroutines.runBlocking
22
19
import net.mullvad.mullvadvpn.lib.common.constant.BuildTypes
@@ -35,17 +32,14 @@ import net.mullvad.mullvadvpn.service.migration.MigrateSplitTunneling
35
32
import net.mullvad.mullvadvpn.service.notifications.ForegroundNotificationManager
36
33
import net.mullvad.mullvadvpn.service.notifications.NotificationChannelFactory
37
34
import net.mullvad.mullvadvpn.service.notifications.NotificationManager
38
- import net.mullvad.mullvadvpn.service.notifications.ShouldBeOnForegroundProvider
39
35
import net.mullvad.talpid.TalpidVpnService
40
36
import org.koin.android.ext.android.getKoin
41
37
import org.koin.core.context.loadKoinModules
42
38
import org.koin.core.qualifier.named
43
39
44
40
private const val RELAYS_FILE = " relays.json"
45
41
46
- class MullvadVpnService : TalpidVpnService (), ShouldBeOnForegroundProvider {
47
- private val _shouldBeOnForeground = MutableStateFlow (false )
48
- override val shouldBeOnForeground: StateFlow <Boolean > = _shouldBeOnForeground
42
+ class MullvadVpnService : TalpidVpnService () {
49
43
50
44
private lateinit var keyguardManager: KeyguardManager
51
45
@@ -74,7 +68,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
74
68
managementService = get()
75
69
76
70
foregroundNotificationHandler =
77
- ForegroundNotificationManager (this @MullvadVpnService, get(), lifecycleScope )
71
+ ForegroundNotificationManager (this @MullvadVpnService, get())
78
72
get<NotificationManager >()
79
73
80
74
apiEndpointConfiguration = get()
@@ -86,8 +80,6 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
86
80
87
81
keyguardManager = getSystemService<KeyguardManager >()!!
88
82
89
- lifecycleScope.launch { foregroundNotificationHandler.start(this @MullvadVpnService) }
90
-
91
83
// TODO We should avoid lifecycleScope.launch (current needed due to InetSocketAddress
92
84
// with intent from API)
93
85
lifecycleScope.launch(context = Dispatchers .IO ) {
@@ -118,7 +110,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
118
110
intent.isFromSystem() || intent?.action == KEY_CONNECT_ACTION -> {
119
111
// Only show on foreground if we have permission
120
112
if (prepare(this ) == null ) {
121
- _shouldBeOnForeground .update { true }
113
+ foregroundNotificationHandler.startForeground()
122
114
}
123
115
lifecycleScope.launch { connectionProxy.connectWithoutPermissionCheck() }
124
116
}
@@ -131,12 +123,12 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
131
123
}
132
124
133
125
override fun onBind (intent : Intent ? ): IBinder {
134
- bindCount.incrementAndGet()
135
- Logger .i(" onBind: $intent " )
126
+ val count = bindCount.incrementAndGet()
127
+ Logger .i(" onBind: $intent , bindCount: $count " )
136
128
137
129
if (intent.isFromSystem()) {
138
- Logger .i(" onBind from system" )
139
- _shouldBeOnForeground .update { true }
130
+ Logger .i(" onBind was from system" )
131
+ foregroundNotificationHandler.startForeground()
140
132
}
141
133
142
134
// We always need to return a binder. If the system binds to our VPN service, VpnService
@@ -145,6 +137,17 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
145
137
return super .onBind(intent) ? : emptyBinder()
146
138
}
147
139
140
+ override fun onRebind (intent : Intent ? ) {
141
+ super .onRebind(intent)
142
+ val count = bindCount.incrementAndGet()
143
+ Logger .i(" onRebind: $intent , bindCount: $count " )
144
+
145
+ if (intent.isFromSystem()) {
146
+ Logger .i(" onRebind from system" )
147
+ foregroundNotificationHandler.startForeground()
148
+ }
149
+ }
150
+
148
151
private fun startDaemon () {
149
152
val apiEndpointConfiguration =
150
153
if (Build .TYPE == BuildTypes .DEBUG ) {
@@ -172,16 +175,18 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
172
175
}
173
176
174
177
override fun onRevoke () {
178
+ Logger .d(" onRevoke" )
175
179
runBlocking { connectionProxy.disconnect() }
176
180
}
177
181
178
182
override fun onUnbind (intent : Intent ): Boolean {
179
183
val count = bindCount.decrementAndGet()
184
+ Logger .i(" onUnbind: $intent , bindCount: $count " )
180
185
181
186
// Foreground?
182
187
if (intent.isFromSystem()) {
183
188
Logger .i(" onUnbind from system" )
184
- _shouldBeOnForeground .update { false }
189
+ foregroundNotificationHandler.stopForeground()
185
190
}
186
191
187
192
if (count == 0 ) {
@@ -203,7 +208,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
203
208
}
204
209
}
205
210
}
206
- return false
211
+ return true
207
212
}
208
213
209
214
override fun onDestroy () {
0 commit comments