summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorAlbin <albin@mullvad.net>2021-11-24 13:52:04 +0100
committerAlbin <albin@mullvad.net>2021-11-26 14:14:42 +0100
commitc20c69fe150593e5f033e120da5f8d421aa3e1d1 (patch)
tree64fbff8881bcd6aedff989b17182b512baf5ff4a /android/src/main
parent1c9767e39c79a0baf969706276b66209e0abfb35 (diff)
downloadmullvadvpn-c20c69fe150593e5f033e120da5f8d421aa3e1d1.tar.xz
mullvadvpn-c20c69fe150593e5f033e120da5f8d421aa3e1d1.zip
Avoid running in foreground when not connected
This change makes the Android service (MullvadVpnService) only run in foreground when connected, compared to previously when it also ran in foreground when the app/ui was in foreground. This change reduces the amount of potential states after the previous work of splitting the app/ui and service into separate processes. It also makes the app/service better aligned with the Android platform, as foreground services only should be used for ongoing operations/work.
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt14
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt4
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt16
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt6
4 files changed, 5 insertions, 35 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 c3e37d6115..dbddc614d3 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt
@@ -92,21 +92,9 @@ class ForegroundNotificationManager(
service.unregisterReceiver(deviceLockListener)
updater.close()
-
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 runUpdater() = GlobalScope.actor<UpdaterMessage>(
Dispatchers.Main,
Channel.UNLIMITED
@@ -132,7 +120,7 @@ class ForegroundNotificationManager(
onForeground = true
}
- private fun updateNotification() {
+ fun updateNotification() {
if (shouldBeOnForeground != onForeground) {
if (shouldBeOnForeground) {
showOnForeground()
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt
index 8045dfbe06..434f6db8ee 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt
@@ -51,11 +51,11 @@ class MullvadTileService : TileService() {
if (secured) {
intent.action = MullvadVpnService.KEY_DISCONNECT_ACTION
+ startService(intent)
} else {
intent.action = MullvadVpnService.KEY_CONNECT_ACTION
+ startForegroundService(intent)
}
-
- startForegroundService(intent)
}
override fun onStopListening() {
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 177db1d712..71067c44d7 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
@@ -61,14 +61,6 @@ class MullvadVpnService : TalpidVpnService() {
}
}
- private var isBound: Boolean by observable(false) { _, _, isBound ->
- notificationManager.lockedToForeground = isUiVisible or isBound
- }
-
- private var isUiVisible: Boolean by observable(false) { _, _, isUiVisible ->
- notificationManager.lockedToForeground = isUiVisible or isBound
- }
-
override fun onCreate() {
super.onCreate()
Log.d(TAG, "Initializing service")
@@ -91,7 +83,6 @@ class MullvadVpnService : TalpidVpnService() {
notificationManager =
ForegroundNotificationManager(this, connectionProxy, keyguardManager).apply {
- acknowledgeStartForegroundService()
accountNumberEvents = endpoint.settingsListener.accountNumberNotifier
}
@@ -121,7 +112,7 @@ class MullvadVpnService : TalpidVpnService() {
val startResult = super.onStartCommand(intent, flags, startId)
var quitCommand = false
- notificationManager.acknowledgeStartForegroundService()
+ notificationManager.updateNotification()
if (!keyguardManager.isDeviceLocked) {
val action = intent?.action
@@ -145,15 +136,11 @@ class MullvadVpnService : TalpidVpnService() {
override fun onBind(intent: Intent): IBinder {
Log.d(TAG, "New connection to service")
- isBound = true
-
return super.onBind(intent) ?: endpoint.messenger.binder
}
override fun onRebind(intent: Intent) {
Log.d(TAG, "Connection to service restored")
- isBound = true
-
if (state == State.Stopping) {
restart()
}
@@ -165,7 +152,6 @@ class MullvadVpnService : TalpidVpnService() {
override fun onUnbind(intent: Intent): Boolean {
Log.d(TAG, "Closed all connections to service")
- isBound = false
if (state != State.Running) {
stop()
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
index 69db9c1243..dfc541d8d5 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
@@ -26,7 +26,6 @@ open class MainActivity : FragmentActivity() {
val problemReport = MullvadProblemReport()
val serviceNotifier = EventNotifier<ServiceConnection?>(null)
- private var isUiVisible = false
private var visibleSecureScreens = HashSet<Fragment>()
private val deviceIsTv by lazy {
@@ -87,11 +86,9 @@ open class MainActivity : FragmentActivity() {
android.util.Log.d("mullvad", "Starting main activity")
super.onStart()
- isUiVisible = true
-
val intent = Intent(this, MullvadVpnService::class.java)
- startForegroundService(intent)
+ startService(intent)
bindService(intent, serviceConnectionManager, 0)
}
@@ -109,7 +106,6 @@ open class MainActivity : FragmentActivity() {
override fun onStop() {
android.util.Log.d("mullvad", "Stoping main activity")
- isUiVisible = false
unbindService(serviceConnectionManager)
super.onStop()