diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-10-27 14:49:38 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-10-28 18:34:44 +0000 |
| commit | 5d8e026c1cf2bdb80ea03c62adfb6823397f6576 (patch) | |
| tree | 174eada510ad3e8cbdcf33d728fb8602c6457020 /android/src/main | |
| parent | f64673ec1480cf8a887372dbd348e565a4e0d480 (diff) | |
| download | mullvadvpn-5d8e026c1cf2bdb80ea03c62adfb6823397f6576.tar.xz mullvadvpn-5d8e026c1cf2bdb80ea03c62adfb6823397f6576.zip | |
Add a `acknowledgeStartForeground` helper method
Used to temporarily place the notification in the foreground by calling
`startForeground` in order to avoid ANR timeouts.
Diffstat (limited to 'android/src/main')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 27 |
1 files changed, 21 insertions, 6 deletions
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..b415f052eb 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -87,15 +87,30 @@ class ForegroundNotificationManager( tunnelStateNotification.visible = false } + 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. + showOnForeground() + + // Restore the notification to its correct state. + updateNotification() + } + + private fun showOnForeground() { + service.startForeground( + TunnelStateNotification.NOTIFICATION_ID, + tunnelStateNotification.build() + ) + + onForeground = true + } + private fun updateNotification() { if (shouldBeOnForeground != onForeground) { if (shouldBeOnForeground) { - service.startForeground( - TunnelStateNotification.NOTIFICATION_ID, - tunnelStateNotification.build() - ) - - onForeground = true + showOnForeground() } else if (!shouldBeOnForeground) { if (Build.VERSION.SDK_INT >= 24) { service.stopForeground(Service.STOP_FOREGROUND_DETACH) |
