diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/ServiceNotification.kt b/mobile/src/main/java/be/mygod/vpnhotspot/ServiceNotification.kt index 6609c2c0..587a0160 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/ServiceNotification.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/ServiceNotification.kt @@ -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>() private val inactiveMap = WeakHashMap>() private val manager = app.getSystemService()!! 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") diff --git a/mobile/src/main/res/values-zh-rCN/strings.xml b/mobile/src/main/res/values-zh-rCN/strings.xml index e727ef85..fb3af0f6 100644 --- a/mobile/src/main/res/values-zh-rCN/strings.xml +++ b/mobile/src/main/res/values-zh-rCN/strings.xml @@ -152,6 +152,7 @@ VPN 共享已启用 VPN 共享服务 + 监视不活跃接口 %d 个设备已连接到 %s diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 4505061f..fe9c1dbd 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -170,8 +170,9 @@ Restart this app to apply this setting. Exit - VPN tethering active + VPN tethering VPN Tethering Service + Monitor Inactive Interfaces %d device connected to %s %d devices connected to %s