diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-05-27 10:57:43 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-05-27 10:57:43 -0300 |
| commit | 9b16816ba628dc33d7f37c8392025c460c1f144d (patch) | |
| tree | fc748dc8cbe25e6d46ca4378f074a84746e93e5d /android/src | |
| parent | 67c7c58b8f1b7f83f330250eb3aae20ccb6f5760 (diff) | |
| parent | 1b03fcd587c1977ad4acc1db916a5fcb46029cbe (diff) | |
| download | mullvadvpn-9b16816ba628dc33d7f37c8392025c460c1f144d.tar.xz mullvadvpn-9b16816ba628dc33d7f37c8392025c460c1f144d.zip | |
Merge branch 'disable-tunnel-actions-while-locked'
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 29 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt | 19 |
2 files changed, 39 insertions, 9 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 fba3f02576..28d32c1c89 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -1,5 +1,6 @@ package net.mullvad.mullvadvpn.service +import android.app.KeyguardManager import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager @@ -24,13 +25,24 @@ val KEY_DISCONNECT_ACTION = "net.mullvad.mullvadvpn.disconnect_action" class ForegroundNotificationManager( val service: MullvadVpnService, - val serviceNotifier: EventNotifier<ServiceInstance?> + val serviceNotifier: EventNotifier<ServiceInstance?>, + val keyguardManager: KeyguardManager ) { private val notificationManager = service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager private val badgeColor = service.resources.getColor(R.color.colorPrimary) + private val deviceLockListener = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val action = intent.action + + if (action == Intent.ACTION_USER_PRESENT || action == Intent.ACTION_SCREEN_OFF) { + deviceIsUnlocked = !keyguardManager.isDeviceLocked + } + } + } + private var connectionProxy: ConnectionProxy? = null set(value) { if (field != value) { @@ -44,7 +56,6 @@ class ForegroundNotificationManager( } } - private var loginListenerId: Int? = null private var settingsListener: SettingsListener? = null set(value) { if (field != value) { @@ -74,6 +85,14 @@ class ForegroundNotificationManager( updateNotification() } + private var deviceIsUnlocked = true + set(value) { + if (field != value) { + field = value + updateNotification() + } + } + private var loggedIn = false set(value) { field = value @@ -195,6 +214,10 @@ class ForegroundNotificationManager( service.apply { registerReceiver(connectReceiver, IntentFilter(KEY_CONNECT_ACTION)) registerReceiver(disconnectReceiver, IntentFilter(KEY_DISCONNECT_ACTION)) + registerReceiver(deviceLockListener, IntentFilter().apply { + addAction(Intent.ACTION_USER_PRESENT) + addAction(Intent.ACTION_SCREEN_OFF) + }) } updateNotification() @@ -263,7 +286,7 @@ class ForegroundNotificationManager( .setContentTitle(service.getString(notificationText)) .setContentIntent(pendingIntent) - if (loggedIn) { + if (loggedIn && deviceIsUnlocked) { builder.addAction(buildTunnelAction()) } 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 132517fe9f..9224246eb0 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -1,5 +1,7 @@ package net.mullvad.mullvadvpn.service +import android.app.KeyguardManager +import android.content.Context import android.content.Intent import android.net.VpnService import android.os.Binder @@ -40,6 +42,7 @@ class MullvadVpnService : TalpidVpnService() { } } + private lateinit var keyguardManager: KeyguardManager private lateinit var notificationManager: ForegroundNotificationManager private lateinit var tunnelStateUpdater: TunnelStateUpdater @@ -67,7 +70,8 @@ class MullvadVpnService : TalpidVpnService() { override fun onCreate() { super.onCreate() - notificationManager = ForegroundNotificationManager(this, serviceNotifier) + keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + notificationManager = ForegroundNotificationManager(this, serviceNotifier, keyguardManager) tunnelStateUpdater = TunnelStateUpdater(this, serviceNotifier) setUp() @@ -75,12 +79,15 @@ class MullvadVpnService : TalpidVpnService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val startResult = super.onStartCommand(intent, flags, startId) - val action = intent?.action - if (action == VpnService.SERVICE_INTERFACE || action == KEY_CONNECT_ACTION) { - pendingAction = PendingAction.Connect - } else if (action == KEY_DISCONNECT_ACTION) { - pendingAction = PendingAction.Disconnect + if (!keyguardManager.isDeviceLocked) { + val action = intent?.action + + if (action == VpnService.SERVICE_INTERFACE || action == KEY_CONNECT_ACTION) { + pendingAction = PendingAction.Connect + } else if (action == KEY_DISCONNECT_ACTION) { + pendingAction = PendingAction.Disconnect + } } return startResult |
