@@ -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,11 +110,12 @@ 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
}
125
117
intent?.action == KEY_DISCONNECT_ACTION -> {
118
+ Logger .d(" Disconnecting!" )
126
119
lifecycleScope.launch { connectionProxy.disconnect() }
127
120
}
128
121
}
@@ -131,12 +124,13 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
131
124
}
132
125
133
126
override fun onBind (intent : Intent ? ): IBinder {
134
- bindCount.incrementAndGet()
135
- Logger .i(" onBind: $intent " )
127
+ Logger .d(" onBind: $intent " )
128
+ val binds = bindCount.incrementAndGet()
129
+ Logger .i(" onBind: $intent , binds: $binds " )
136
130
137
131
if (intent.isFromSystem()) {
138
132
Logger .i(" onBind from system" )
139
- _shouldBeOnForeground .update { true }
133
+ foregroundNotificationHandler.startForeground()
140
134
}
141
135
142
136
// We always need to return a binder. If the system binds to our VPN service, VpnService
@@ -145,6 +139,17 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
145
139
return super .onBind(intent) ? : emptyBinder()
146
140
}
147
141
142
+ override fun onRebind (intent : Intent ? ) {
143
+ super .onRebind(intent)
144
+ Logger .i(" onRebind: $intent , binds: ${bindCount.get()} " )
145
+ val binds = bindCount.incrementAndGet()
146
+
147
+ if (intent.isFromSystem()) {
148
+ Logger .i(" onRebind from system" )
149
+ foregroundNotificationHandler.startForeground()
150
+ }
151
+ }
152
+
148
153
private fun startDaemon () {
149
154
val apiEndpointConfiguration =
150
155
if (Build .TYPE == BuildTypes .DEBUG ) {
@@ -172,16 +177,19 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
172
177
}
173
178
174
179
override fun onRevoke () {
180
+ Logger .d(" onRevoke" )
175
181
runBlocking { connectionProxy.disconnect() }
176
182
}
177
183
178
184
override fun onUnbind (intent : Intent ): Boolean {
185
+ Logger .i(" onUnbind, preGet! ${bindCount.get()} : " )
179
186
val count = bindCount.decrementAndGet()
187
+ Logger .i(" onUnbind: $intent , binds: $count " )
180
188
181
189
// Foreground?
182
190
if (intent.isFromSystem()) {
183
191
Logger .i(" onUnbind from system" )
184
- _shouldBeOnForeground .update { false }
192
+ foregroundNotificationHandler.stopForeground()
185
193
}
186
194
187
195
if (count == 0 ) {
@@ -203,7 +211,7 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider {
203
211
}
204
212
}
205
213
}
206
- return false
214
+ return true
207
215
}
208
216
209
217
override fun onDestroy () {
0 commit comments