Use one notification for two services
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package be.mygod.vpnhotspot
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.support.v4.app.NotificationCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
|
||||
object ServiceNotification {
|
||||
private const val CHANNEL = "tethering"
|
||||
private const val CHANNEL_ID = 1
|
||||
|
||||
private val deviceCountsMap = HashMap<Service, Map<String, Int>>()
|
||||
private val manager = app.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
private fun buildNotification(context: Context): Notification {
|
||||
val builder = NotificationCompat.Builder(context, CHANNEL)
|
||||
.setWhen(0)
|
||||
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
|
||||
.setContentTitle("VPN tethering active")
|
||||
.setSmallIcon(R.drawable.ic_device_wifi_tethering)
|
||||
.setContentIntent(PendingIntent.getActivity(context, 0,
|
||||
Intent(context, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
||||
val deviceCounts = deviceCountsMap.values.flatMap { it.entries }.sortedBy { it.key }
|
||||
return when (deviceCounts.size) {
|
||||
0 -> builder.build()
|
||||
1 -> {
|
||||
val (dev, size) = deviceCounts.single()
|
||||
builder.setContentText(context.resources.getQuantityString(R.plurals.notification_connected_devices,
|
||||
size, size, dev))
|
||||
.build()
|
||||
}
|
||||
else -> {
|
||||
val deviceCount = deviceCounts.sumBy { it.value }
|
||||
NotificationCompat.BigTextStyle(builder
|
||||
.setContentText(context.resources.getQuantityString(R.plurals.notification_connected_devices,
|
||||
deviceCount, deviceCount,
|
||||
context.resources.getQuantityString(R.plurals.notification_interfaces,
|
||||
deviceCounts.size, deviceCounts.size))))
|
||||
.bigText(deviceCounts.joinToString("\n") { (dev, size) ->
|
||||
context.resources.getQuantityString(R.plurals.notification_connected_devices,
|
||||
size, size, dev)
|
||||
})
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun startForeground(service: Service, deviceCounts: Map<String, Int>) {
|
||||
deviceCountsMap[service] = deviceCounts
|
||||
service.startForeground(CHANNEL_ID, buildNotification(service))
|
||||
}
|
||||
fun stopForeground(service: Service) {
|
||||
deviceCountsMap.remove(service)
|
||||
if (deviceCountsMap.isEmpty()) service.stopForeground(true) else {
|
||||
service.stopForeground(false)
|
||||
manager.notify(CHANNEL_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)
|
||||
// remove old service channels
|
||||
manager.deleteNotificationChannel("hotspot")
|
||||
manager.deleteNotificationChannel("repeater")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user