diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-10-28 16:06:10 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-10-28 16:06:10 -0300 |
| commit | 5c9d8652fc670a24887ecc6a90d9f452ba5bd343 (patch) | |
| tree | d6d0d45c0125df44a48ac1064d100ccdb44d6245 | |
| parent | f64673ec1480cf8a887372dbd348e565a4e0d480 (diff) | |
| parent | 721748dd4e6c0e6a6dcc9584ca8c6480f3b9c66d (diff) | |
| download | mullvadvpn-5c9d8652fc670a24887ecc6a90d9f452ba5bd343.tar.xz mullvadvpn-5c9d8652fc670a24887ecc6a90d9f452ba5bd343.zip | |
Merge branch 'fix-missing-call-to-start-foreground'
3 files changed, 39 insertions, 14 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a300c23d3c..1a994a6384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,8 @@ Line wrap the file at 100 chars. Th settings tile. - Fix app starting by itself sometimes. - Fix apps not being excluded from the tunnel sometimes if auto-connect was enabled. +- Fix crash that happened sometimes when closing the app or when requesting from the notification + or the quick-settings tile for the app to connect or disconnect. #### Windows - Fix log output encoding for Windows modules. diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt index 0329e156bf..c402881488 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -87,23 +87,42 @@ class ForegroundNotificationManager( tunnelStateNotification.visible = false } - private fun updateNotification() { - if (shouldBeOnForeground != onForeground) { - if (shouldBeOnForeground) { - service.startForeground( - TunnelStateNotification.NOTIFICATION_ID, - tunnelStateNotification.build() - ) + fun acknowledgeStartForegroundService() { + // When sending start commands to the service, it is necessary to request the service to be + // on the foreground. With such request, when the service is started it must be placed on + // the foreground with a call to startForeground before a timeout expires, otherwise Android + // kills the app. + synchronized(this) { + showOnForeground() + } + + // Restore the notification to its correct state. + updateNotification() + } + + private fun showOnForeground() { + service.startForeground( + TunnelStateNotification.NOTIFICATION_ID, + tunnelStateNotification.build() + ) - onForeground = true - } else if (!shouldBeOnForeground) { - if (Build.VERSION.SDK_INT >= 24) { - service.stopForeground(Service.STOP_FOREGROUND_DETACH) + onForeground = true + } + + private fun updateNotification() { + synchronized(this) { + if (shouldBeOnForeground != onForeground) { + if (shouldBeOnForeground) { + showOnForeground() } else { - service.stopForeground(false) - } + if (Build.VERSION.SDK_INT >= 24) { + service.stopForeground(Service.STOP_FOREGROUND_DETACH) + } else { + service.stopForeground(false) + } - onForeground = false + onForeground = false + } } } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index 439216e897..819886d1f6 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -94,6 +94,8 @@ class MullvadVpnService : TalpidVpnService() { notificationManager = ForegroundNotificationManager(this, serviceNotifier, keyguardManager) tunnelStateUpdater = TunnelStateUpdater(this, serviceNotifier) + notificationManager.acknowledgeStartForegroundService() + setUp() } @@ -102,6 +104,8 @@ class MullvadVpnService : TalpidVpnService() { val startResult = super.onStartCommand(intent, flags, startId) var quitCommand = false + notificationManager.acknowledgeStartForegroundService() + if (!keyguardManager.isDeviceLocked) { val action = intent?.action |
