Hide notification when service is inactive
This commit is contained in:
@@ -12,25 +12,29 @@ import be.mygod.vpnhotspot.App.Companion.app
|
||||
import java.util.*
|
||||
|
||||
object ServiceNotification {
|
||||
private const val CHANNEL = "tethering"
|
||||
private const val CHANNEL_ID = 1
|
||||
private const val CHANNEL_ACTIVE = "tethering"
|
||||
private const val CHANNEL_INACTIVE = "tethering-inactive"
|
||||
private const val NOTIFICATION_ID = 1
|
||||
|
||||
private val deviceCountsMap = WeakHashMap<Service, Map<String, Int>>()
|
||||
private val inactiveMap = WeakHashMap<Service, List<String>>()
|
||||
private val manager = app.getSystemService<NotificationManager>()!!
|
||||
|
||||
private fun buildNotification(context: Context): Notification {
|
||||
val builder = NotificationCompat.Builder(context, CHANNEL)
|
||||
.setWhen(0)
|
||||
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
|
||||
.setContentTitle(context.getText(R.string.notification_tethering_title))
|
||||
.setSmallIcon(R.drawable.ic_quick_settings_tile_on)
|
||||
.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
val deviceCounts = deviceCountsMap.values.flatMap { it.entries }.sortedBy { it.key }
|
||||
val inactive = inactiveMap.values.flatten()
|
||||
val isInactive = inactive.isNotEmpty() && deviceCounts.isEmpty()
|
||||
val builder = NotificationCompat.Builder(context, if (isInactive) CHANNEL_INACTIVE else CHANNEL_ACTIVE).apply {
|
||||
setWhen(0)
|
||||
setCategory(NotificationCompat.CATEGORY_SERVICE)
|
||||
color = ContextCompat.getColor(context, R.color.colorPrimary)
|
||||
setContentTitle(context.getText(R.string.notification_tethering_title))
|
||||
setSmallIcon(R.drawable.ic_quick_settings_tile_on)
|
||||
setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
|
||||
setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
priority = if (isInactive) NotificationCompat.PRIORITY_MIN else NotificationCompat.PRIORITY_LOW
|
||||
}
|
||||
var lines = deviceCounts.map { (dev, size) ->
|
||||
context.resources.getQuantityString(R.plurals.notification_connected_devices, size, size, dev)
|
||||
}
|
||||
@@ -54,23 +58,29 @@ object ServiceNotification {
|
||||
synchronized(this) {
|
||||
deviceCountsMap[service] = deviceCounts
|
||||
if (inactive.isEmpty()) inactiveMap.remove(service) else inactiveMap[service] = inactive
|
||||
service.startForeground(CHANNEL_ID, buildNotification(service))
|
||||
service.startForeground(NOTIFICATION_ID, buildNotification(service))
|
||||
}
|
||||
}
|
||||
fun stopForeground(service: Service) = synchronized(this) {
|
||||
deviceCountsMap.remove(service)
|
||||
if (deviceCountsMap.isEmpty()) service.stopForeground(true) else {
|
||||
service.stopForeground(false)
|
||||
manager.notify(CHANNEL_ID, buildNotification(service))
|
||||
manager.notify(NOTIFICATION_ID, buildNotification(service))
|
||||
}
|
||||
}
|
||||
|
||||
fun updateNotificationChannels() {
|
||||
if (Build.VERSION.SDK_INT >= 26) @TargetApi(26) {
|
||||
val tethering = NotificationChannel(CHANNEL,
|
||||
app.getText(R.string.notification_channel_tethering), NotificationManager.IMPORTANCE_LOW)
|
||||
tethering.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
manager.createNotificationChannel(tethering)
|
||||
NotificationChannel(CHANNEL_ACTIVE,
|
||||
app.getText(R.string.notification_channel_tethering), NotificationManager.IMPORTANCE_LOW).apply {
|
||||
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
manager.createNotificationChannel(this)
|
||||
}
|
||||
NotificationChannel(CHANNEL_INACTIVE,
|
||||
app.getText(R.string.notification_channel_monitor), NotificationManager.IMPORTANCE_LOW).apply {
|
||||
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
manager.createNotificationChannel(this)
|
||||
}
|
||||
// remove old service channels
|
||||
manager.deleteNotificationChannel("hotspot")
|
||||
manager.deleteNotificationChannel("repeater")
|
||||
|
||||
@@ -152,6 +152,7 @@
|
||||
|
||||
<string name="notification_tethering_title">VPN 共享已启用</string>
|
||||
<string name="notification_channel_tethering">VPN 共享服务</string>
|
||||
<string name="notification_channel_monitor">监视不活跃接口</string>
|
||||
<plurals name="notification_connected_devices">
|
||||
<item quantity="other">%d 个设备已连接到 %s</item>
|
||||
</plurals>
|
||||
|
||||
@@ -170,8 +170,9 @@
|
||||
<string name="settings_restart_required">Restart this app to apply this setting.</string>
|
||||
<string name="settings_exit_app">Exit</string>
|
||||
|
||||
<string name="notification_tethering_title">VPN tethering active</string>
|
||||
<string name="notification_tethering_title">VPN tethering</string>
|
||||
<string name="notification_channel_tethering">VPN Tethering Service</string>
|
||||
<string name="notification_channel_monitor">Monitor Inactive Interfaces</string>
|
||||
<plurals name="notification_connected_devices">
|
||||
<item quantity="one">%d device connected to %s</item>
|
||||
<item quantity="other">%d devices connected to %s</item>
|
||||
|
||||
Reference in New Issue
Block a user