diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-08 16:59:30 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-17 14:15:15 +0000 |
| commit | d9f58f48f13e73231370ed83638206ec23d2140c (patch) | |
| tree | 9f8b4fdca5faffad93cc195e790076140f15ecac /android/src | |
| parent | 3cac338e35e1dd1b3b021874de792e7c9467d22c (diff) | |
| download | mullvadvpn-d9f58f48f13e73231370ed83638206ec23d2140c.tar.xz mullvadvpn-d9f58f48f13e73231370ed83638206ec23d2140c.zip | |
Use `TunnelStateNotification`
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 184 |
1 files changed, 16 insertions, 168 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 1b2019b07e..c352361e76 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -1,36 +1,23 @@ package net.mullvad.mullvadvpn.service import android.app.KeyguardManager -import android.app.Notification -import android.app.NotificationChannel -import android.app.NotificationManager -import android.app.PendingIntent import android.app.Service import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build -import android.support.v4.app.NotificationCompat import kotlin.properties.Delegates.observable -import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.model.TunnelState -import net.mullvad.mullvadvpn.ui.MainActivity -import net.mullvad.talpid.tunnel.ActionAfterDisconnect +import net.mullvad.mullvadvpn.service.notifications.TunnelStateNotification import net.mullvad.talpid.util.EventNotifier -val CHANNEL_ID = "vpn_tunnel_status" -val FOREGROUND_NOTIFICATION_ID: Int = 1 - class ForegroundNotificationManager( val service: MullvadVpnService, 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 tunnelStateNotification = TunnelStateNotification(service) private val deviceLockListener = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -62,116 +49,25 @@ class ForegroundNotificationManager( } } - private var onForeground = false - private var reconnecting = false - private var showingReconnecting = false - private var tunnelState by observable<TunnelState>(TunnelState.Disconnected()) { _, _, state -> - reconnecting = - (state is TunnelState.Disconnecting && - state.actionAfterDisconnect == ActionAfterDisconnect.Reconnect) || - (state is TunnelState.Connecting && reconnecting) - + tunnelStateNotification.tunnelState = state updateNotification() } private var deviceIsUnlocked by observable(!keyguardManager.isDeviceLocked) { _, _, _ -> - updateNotification() + updateNotificationAction() } - private var loggedIn by observable(false) { _, _, _ -> updateNotification() } + private var loggedIn by observable(false) { _, _, _ -> updateNotificationAction() } + + private var onForeground = false private val shouldBeOnForeground get() = lockedToForeground || !(tunnelState is TunnelState.Disconnected) - private val notificationText: Int - get() { - val state = tunnelState - - return when (state) { - is TunnelState.Disconnected -> R.string.unsecured - is TunnelState.Connecting -> { - if (reconnecting) { - R.string.reconnecting - } else { - R.string.connecting - } - } - is TunnelState.Connected -> R.string.secured - is TunnelState.Disconnecting -> { - when (state.actionAfterDisconnect) { - ActionAfterDisconnect.Reconnect -> R.string.reconnecting - else -> R.string.disconnecting - } - } - is TunnelState.Error -> { - if (state.errorState.isBlocking) { - R.string.blocking_all_connections - } else { - R.string.critical_error - } - } - } - } - - private val tunnelActionText: Int - get() { - val state = tunnelState - - return when (state) { - is TunnelState.Disconnected -> R.string.connect - is TunnelState.Connecting -> R.string.cancel - is TunnelState.Connected -> R.string.disconnect - is TunnelState.Disconnecting -> { - when (state.actionAfterDisconnect) { - ActionAfterDisconnect.Reconnect -> R.string.cancel - else -> R.string.connect - } - } - is TunnelState.Error -> { - if (state.errorState.isBlocking) { - R.string.disconnect - } else { - R.string.dismiss - } - } - } - } - - private val tunnelActionKey: String - get() { - val state = tunnelState - - return when (state) { - is TunnelState.Disconnected -> MullvadVpnService.KEY_CONNECT_ACTION - is TunnelState.Connecting -> MullvadVpnService.KEY_DISCONNECT_ACTION - is TunnelState.Connected -> MullvadVpnService.KEY_DISCONNECT_ACTION - is TunnelState.Disconnecting -> { - when (state.actionAfterDisconnect) { - ActionAfterDisconnect.Reconnect -> MullvadVpnService.KEY_DISCONNECT_ACTION - else -> MullvadVpnService.KEY_CONNECT_ACTION - } - } - is TunnelState.Error -> MullvadVpnService.KEY_DISCONNECT_ACTION - } - } - - private val tunnelActionIcon: Int - get() { - if (tunnelActionKey == MullvadVpnService.KEY_CONNECT_ACTION) { - return R.drawable.icon_notification_connect - } else { - return R.drawable.icon_notification_disconnect - } - } - - var lockedToForeground by observable(false) { _, _, _ -> updateNotificationForegroundStatus() } + var lockedToForeground by observable(false) { _, _, _ -> updateNotification() } init { - if (Build.VERSION.SDK_INT >= 26) { - initChannel() - } - serviceNotifier.subscribe(this) { newServiceInstance -> connectionProxy = newServiceInstance?.connectionProxy settingsListener = newServiceInstance?.settingsListener @@ -194,32 +90,17 @@ class ForegroundNotificationManager( service.unregisterReceiver(deviceLockListener) - notificationManager.cancel(FOREGROUND_NOTIFICATION_ID) - } - - private fun initChannel() { - val channelName = service.getString(R.string.foreground_notification_channel_name) - val importance = NotificationManager.IMPORTANCE_MIN - val channel = NotificationChannel(CHANNEL_ID, channelName, importance).apply { - description = service.getString(R.string.foreground_notification_channel_description) - setShowBadge(true) - } - - notificationManager.createNotificationChannel(channel) + tunnelStateNotification.visible = false } private fun updateNotification() { - if (!reconnecting || !showingReconnecting) { - notificationManager.notify(FOREGROUND_NOTIFICATION_ID, buildNotification()) - } - - updateNotificationForegroundStatus() - } - - private fun updateNotificationForegroundStatus() { if (shouldBeOnForeground != onForeground) { if (shouldBeOnForeground) { - service.startForeground(FOREGROUND_NOTIFICATION_ID, buildNotification()) + service.startForeground( + TunnelStateNotification.NOTIFICATION_ID, + tunnelStateNotification.build() + ) + onForeground = true } else if (!shouldBeOnForeground) { if (Build.VERSION.SDK_INT >= 24) { @@ -233,40 +114,7 @@ class ForegroundNotificationManager( } } - private fun buildNotification(): Notification { - val intent = Intent(service, MainActivity::class.java) - .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) - .setAction(Intent.ACTION_MAIN) - - val pendingIntent = - PendingIntent.getActivity(service, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT) - - val builder = NotificationCompat.Builder(service, CHANNEL_ID) - .setSmallIcon(R.drawable.small_logo_black) - .setColor(badgeColor) - .setContentTitle(service.getString(notificationText)) - .setContentIntent(pendingIntent) - - if (loggedIn && deviceIsUnlocked) { - builder.addAction(buildTunnelAction()) - } - - return builder.build() - } - - private fun buildTunnelAction(): NotificationCompat.Action { - val intent = Intent(tunnelActionKey).setPackage("net.mullvad.mullvadvpn") - val flags = PendingIntent.FLAG_UPDATE_CURRENT - - val pendingIntent = if (Build.VERSION.SDK_INT >= 26) { - PendingIntent.getForegroundService(service, 1, intent, flags) - } else { - PendingIntent.getService(service, 1, intent, flags) - } - - val icon = tunnelActionIcon - val label = service.getString(tunnelActionText) - - return NotificationCompat.Action(icon, label, pendingIntent) + private fun updateNotificationAction() { + tunnelStateNotification.showAction = loggedIn && deviceIsUnlocked } } |
