fix: lower notification importance, group notifications

closes #159
This commit is contained in:
Zane Schepke
2025-10-16 10:18:13 -04:00
parent a451e962b0
commit a2139fa2fc
7 changed files with 39 additions and 37 deletions
@@ -17,9 +17,11 @@ interface NotificationManager {
actions: Collection<NotificationCompat.Action> = emptyList(),
description: String = "",
showTimestamp: Boolean = true,
importance: Int = NotificationManager.IMPORTANCE_HIGH,
importance: Int = NotificationManager.IMPORTANCE_LOW,
onGoing: Boolean = false,
onlyAlertOnce: Boolean = true,
groupKey: String? = null,
isGroupSummary: Boolean = false,
): Notification
fun createNotification(
@@ -28,9 +30,11 @@ interface NotificationManager {
actions: Collection<NotificationCompat.Action> = emptyList(),
description: StringValue,
showTimestamp: Boolean = true,
importance: Int = NotificationManager.IMPORTANCE_HIGH,
importance: Int = NotificationManager.IMPORTANCE_LOW,
onGoing: Boolean = false,
onlyAlertOnce: Boolean = true,
groupKey: String? = null,
isGroupSummary: Boolean = false,
): Notification
fun createNotificationAction(
@@ -43,6 +47,8 @@ interface NotificationManager {
fun show(notificationId: Int, notification: Notification)
companion object {
const val VPN_GROUP_KEY = "VPN_GROUP"
const val AUTO_TUNNEL_GROUP_KEY = "AUTO_TUNNEL_GROUP"
const val AUTO_TUNNEL_LOCATION_PERMISSION_ID = 123
const val AUTO_TUNNEL_LOCATION_SERVICES_ID = 124
// For auto tunnel foreground notification
@@ -37,6 +37,7 @@ constructor(
error.toStringRes(),
)
},
groupKey = NotificationManager.VPN_GROUP_KEY,
)
notificationManager.show(
NotificationManager.TUNNEL_ERROR_NOTIFICATION_ID,
@@ -53,6 +54,7 @@ constructor(
WireGuardNotification.NotificationChannels.VPN,
title = StringValue.DynamicString(tunName),
description = message.toStringValue(),
groupKey = NotificationManager.VPN_GROUP_KEY,
)
notificationManager.show(
NotificationManager.TUNNEL_MESSAGES_NOTIFICATION_ID,
@@ -3,12 +3,10 @@ package com.zaneschepke.wireguardautotunnel.core.notification
import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
@@ -22,7 +20,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
class WireGuardNotification @Inject constructor(@ApplicationContext override val context: Context) :
com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager {
NotificationManager {
enum class NotificationChannels {
VPN,
@@ -40,8 +38,10 @@ class WireGuardNotification @Inject constructor(@ApplicationContext override val
importance: Int,
onGoing: Boolean,
onlyAlertOnce: Boolean,
groupKey: String?,
isGroupSummary: Boolean,
): Notification {
notificationManager.createNotificationChannel(channel.asChannel())
notificationManager.createNotificationChannel(channel.asChannel(importance))
return channel
.asBuilder()
.apply {
@@ -59,9 +59,15 @@ class WireGuardNotification @Inject constructor(@ApplicationContext override val
setContentText(description)
setOnlyAlertOnce(onlyAlertOnce)
setOngoing(onGoing)
setPriority(NotificationCompat.PRIORITY_HIGH)
setPriority(NotificationCompat.PRIORITY_LOW)
setShowWhen(showTimestamp)
setSmallIcon(R.drawable.ic_notification)
if (groupKey != null) {
setGroup(groupKey)
if (isGroupSummary) {
setGroupSummary(true)
}
}
}
.build()
}
@@ -75,6 +81,8 @@ class WireGuardNotification @Inject constructor(@ApplicationContext override val
importance: Int,
onGoing: Boolean,
onlyAlertOnce: Boolean,
groupKey: String?,
isGroupSummary: Boolean,
): Notification {
return createNotification(
channel,
@@ -142,34 +150,24 @@ class WireGuardNotification @Inject constructor(@ApplicationContext override val
}
}
private fun NotificationChannels.asChannel(): NotificationChannel {
private fun NotificationChannels.asChannel(importance: Int): NotificationChannel {
return when (this) {
NotificationChannels.VPN -> {
NotificationChannel(
context.getString(R.string.vpn_channel_id),
context.getString(R.string.vpn_channel_name),
NotificationManager.IMPORTANCE_HIGH,
importance,
)
.apply {
description = context.getString(R.string.vpn_channel_description)
enableLights(true)
lightColor = Color.WHITE
enableVibration(false)
vibrationPattern = longArrayOf(100, 200, 300)
}
.apply { description = context.getString(R.string.vpn_channel_description) }
}
NotificationChannels.AUTO_TUNNEL -> {
NotificationChannel(
context.getString(R.string.auto_tunnel_channel_id),
context.getString(R.string.auto_tunnel_channel_name),
NotificationManager.IMPORTANCE_HIGH,
importance,
)
.apply {
description = context.getString(R.string.auto_tunnel_channel_description)
enableLights(true)
lightColor = Color.WHITE
enableVibration(false)
vibrationPattern = longArrayOf(100, 200, 300)
}
}
}
@@ -118,6 +118,8 @@ abstract class BaseTunnelForegroundService : LifecycleService(), TunnelService {
)
),
onGoing = true,
groupKey = NotificationManager.VPN_GROUP_KEY,
isGroupSummary = true,
)
}
@@ -129,6 +131,8 @@ abstract class BaseTunnelForegroundService : LifecycleService(), TunnelService {
listOf(
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0)
),
groupKey = NotificationManager.VPN_GROUP_KEY,
isGroupSummary = true,
)
}
@@ -136,6 +140,8 @@ abstract class BaseTunnelForegroundService : LifecycleService(), TunnelService {
return notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN,
title = getString(R.string.tunnel_starting),
groupKey = NotificationManager.VPN_GROUP_KEY,
isGroupSummary = true,
)
}
}
@@ -112,6 +112,8 @@ class AutoTunnelService : LifecycleService() {
)
),
onGoing = true,
groupKey = NotificationManager.AUTO_TUNNEL_GROUP_KEY,
isGroupSummary = true,
)
ServiceCompat.startForeground(
this,
@@ -3,20 +3,12 @@ package com.zaneschepke.wireguardautotunnel.ui.common.banner
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Warning
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.button.SurfaceRow
import com.zaneschepke.wireguardautotunnel.ui.theme.Straw
@@ -34,14 +26,10 @@ fun WarningBanner(
title = title,
modifier = modifier,
leading = {
Icon(
Icons.Outlined.Warning,
stringResource(R.string.warning),
tint = Straw,
)
Icon(Icons.Outlined.Warning, stringResource(R.string.warning), tint = Straw)
},
trailing = { trailing?.invoke() },
onClick = onClick
onClick = onClick,
)
}
}
@@ -112,7 +112,7 @@ fun WifiSettingsScreen(viewModel: AutoTunnelViewModel = hiltViewModel()) {
)
}
},
onClick = { context.launchLocationServicesSettings() }
onClick = { context.launchLocationServicesSettings() },
)
WarningBanner(
stringResource(R.string.location_permissions_missing),
@@ -126,7 +126,7 @@ fun WifiSettingsScreen(viewModel: AutoTunnelViewModel = hiltViewModel()) {
)
}
},
onClick = { showLocationDialog = true }
onClick = { showLocationDialog = true },
)
}
Column {