diff options
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt new file mode 100644 index 0000000000..2546c8955f --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationChannel.kt @@ -0,0 +1,61 @@ +package net.mullvad.mullvadvpn.service.notifications + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.os.Build +import android.support.v4.app.NotificationCompat +import net.mullvad.mullvadvpn.R + +class NotificationChannel( + val context: Context, + val id: String, + val name: Int, + val description: Int, + val importance: Int +) { + private val badgeColor by lazy { + context.resources.getColor(R.color.colorPrimary) + } + + val notificationManager = + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + init { + if (Build.VERSION.SDK_INT >= 26) { + val channelName = context.getString(name) + val channelDescription = context.getString(description) + + val channel = NotificationChannel(id, channelName, importance).apply { + description = channelDescription + setShowBadge(true) + } + + notificationManager.createNotificationChannel(channel) + } + } + + fun buildNotification(intent: PendingIntent, title: Int): Notification { + return buildNotification(intent, title, emptyList()) + } + + fun buildNotification( + pendingIntent: PendingIntent, + title: Int, + actions: List<NotificationCompat.Action> + ): Notification { + val builder = NotificationCompat.Builder(context, id) + .setSmallIcon(R.drawable.small_logo_black) + .setColor(badgeColor) + .setContentTitle(context.getString(title)) + .setContentIntent(pendingIntent) + + for (action in actions) { + builder.addAction(action) + } + + return builder.build() + } +} |
