OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/notifications/TelegramNotification.kt

88 lines
2.5 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram.notifications
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Intent
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import net.osmand.telegram.MainActivity
import net.osmand.telegram.TelegramApplication
abstract class TelegramNotification(protected var app: TelegramApplication, val groupName: String) {
2018-06-11 16:12:03 +02:00
companion object {
2018-06-11 18:57:33 +02:00
const val LOCATION_NOTIFICATION_SERVICE_ID = 6
2018-06-11 16:12:03 +02:00
2018-06-11 18:57:33 +02:00
const val WEAR_LOCATION_NOTIFICATION_SERVICE_ID = 1006
2018-06-11 16:12:03 +02:00
}
protected var ongoing = true
protected var color: Int = 0
protected var icon: Int = 0
abstract val type: NotificationType
abstract val telegramNotificationId: Int
abstract val telegramWearableNotificationId: Int
abstract val priority: Int
abstract val isActive: Boolean
abstract val isEnabled: Boolean
enum class NotificationType {
2018-06-11 18:57:33 +02:00
LOCATION,
2018-06-11 16:12:03 +02:00
}
@SuppressLint("InlinedApi")
protected fun createBuilder(wearable: Boolean): NotificationCompat.Builder {
val contentIntent = Intent(app, MainActivity::class.java)
val contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
app.notificationHelper.createNotificationChannel()
}
val builder = NotificationCompat.Builder(app, NotificationHelper.NOTIFICATION_CHANEL_ID)
.setVisibility(android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(priority)
.setOngoing(ongoing && !wearable)
.setContentIntent(contentPendingIntent)
.setGroup(groupName).setGroupSummary(!wearable)
if (color != 0) {
builder.color = color
}
if (icon != 0) {
builder.setSmallIcon(icon)
}
return builder
}
abstract fun buildNotification(wearable: Boolean): NotificationCompat.Builder
private fun notifyWearable(notificationManager: NotificationManagerCompat) {
val wearNotificationBuilder = buildNotification(true)
val wearNotification = wearNotificationBuilder.build()
notificationManager.notify(telegramWearableNotificationId, wearNotification)
}
fun refreshNotification(): Boolean {
val notificationManager = NotificationManagerCompat.from(app)
if (isEnabled) {
val notificationBuilder = buildNotification(false)
val notification = notificationBuilder.build()
notificationManager.notify(telegramNotificationId, notification)
notifyWearable(notificationManager)
return true
} else {
notificationManager.cancel(telegramNotificationId)
}
return false
}
}