add tunnel notification
This commit is contained in:
parent
f047a1e1b8
commit
99f6b438ab
|
@ -2,10 +2,12 @@ package com.zaneschepke.wireguardautotunnel.service.notification
|
|||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification.NotificationChannels
|
||||
|
||||
interface NotificationService {
|
||||
val context: Context
|
||||
fun createNotification(
|
||||
channel: NotificationChannels,
|
||||
title: String = "",
|
||||
|
@ -19,8 +21,13 @@ interface NotificationService {
|
|||
|
||||
fun createNotificationAction(action: NotificationAction): NotificationCompat.Action
|
||||
|
||||
fun remove(notificationId: Int)
|
||||
|
||||
fun show(notificationId: Int, notification: Notification)
|
||||
|
||||
companion object {
|
||||
const val KERNEL_SERVICE_NOTIFICATION_ID = 123
|
||||
const val AUTO_TUNNEL_NOTIFICATION_ID = 122
|
||||
const val VPN_NOTIFICATION_ID = 100
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import javax.inject.Inject
|
|||
class WireGuardNotification
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
@ApplicationContext override val context: Context,
|
||||
) : NotificationService {
|
||||
|
||||
enum class NotificationChannels {
|
||||
|
@ -28,8 +28,7 @@ constructor(
|
|||
AUTO_TUNNEL,
|
||||
}
|
||||
|
||||
private val notificationManager =
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val notificationManager = NotificationManagerCompat.from(context)
|
||||
|
||||
override fun createNotification(
|
||||
channel: NotificationChannels,
|
||||
|
@ -72,9 +71,12 @@ constructor(
|
|||
).build()
|
||||
}
|
||||
|
||||
fun Notification.show(notificationId: Int = defaultId()) {
|
||||
val notification = this
|
||||
with(NotificationManagerCompat.from(context)) {
|
||||
override fun remove(notificationId: Int) {
|
||||
notificationManager.cancel(notificationId)
|
||||
}
|
||||
|
||||
override fun show(notificationId: Int, notification: Notification) {
|
||||
with(notificationManager) {
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
return
|
||||
}
|
||||
|
@ -82,14 +84,6 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun Notification.defaultId(): Int {
|
||||
return when (this.channelId) {
|
||||
context.getString(R.string.vpn_channel_id) -> 100
|
||||
context.getString(R.string.auto_tunnel_channel_id) -> 101
|
||||
else -> 102
|
||||
}
|
||||
}
|
||||
|
||||
fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
|
||||
return when (this) {
|
||||
NotificationChannels.VPN -> {
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
|||
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics
|
||||
|
@ -28,6 +29,9 @@ import kotlinx.coroutines.sync.Mutex
|
|||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService.Companion.VPN_NOTIFICATION_ID
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
|
@ -118,6 +122,16 @@ constructor(
|
|||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
||||
startActiveTunnelJobs()
|
||||
if (it.isUp()) appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
with(notificationService) {
|
||||
val notification = createNotification(
|
||||
WireGuardNotification.NotificationChannels.VPN,
|
||||
title = "${context.getString(R.string.tunnel_running)} - ${tunnelConfig.name}",
|
||||
actions = listOf(
|
||||
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||
),
|
||||
)
|
||||
show(VPN_NOTIFICATION_ID, notification)
|
||||
}
|
||||
updateTunnelState(it, tunnelConfig)
|
||||
}
|
||||
}.onFailure {
|
||||
|
@ -136,6 +150,7 @@ constructor(
|
|||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
||||
updateTunnelState(it, null)
|
||||
onStop(tunnelConfig)
|
||||
notificationService.remove(VPN_NOTIFICATION_ID)
|
||||
stopBackgroundService()
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
|
@ -214,7 +229,7 @@ constructor(
|
|||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
|
||||
private suspend fun stopBackgroundService() {
|
||||
private fun stopBackgroundService() {
|
||||
serviceManager.stopBackgroundService()
|
||||
serviceManager.updateTunnelTile()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue